package com.cn.test.string; public class StringTest { public static void main(String[] args) { String str1 = getStringSum("999999999999999999999999999999999999999999999999999999999999999999999999999999","911911111111111111111111111111111111111111111111111111119999999999"); System.out.println(str1); } public static String getStringSum(String num1,String num2) { StringBuffer result = new StringBuffer(); //反轉字符串 123 --> 321 8912 --> 2198 num1 = new StringBuffer(num1).reverse().toString(); num2 = new StringBuffer(num2).reverse().toString(); int len1 = num1.length(); int len2 = num2.length(); int maxLen = len1 > len2 ? len1 : len2; int nTakeOver = 0 ; //進位 //2.把兩個字符串補齊,即短字符串的高位用0補齊 if(len1 < len2){ // 3210 for(int i = len1;i < len2; i++) { num1 += "0"; } } else if (len1 > len2){ for(int i = len2 ; i < len1 ; i++) { num2 += "0"; } } System.out.println(num1); System.out.println(num2); //3.逐位相加 3210 2198 for(int i=0; i<maxLen; i++) { int nSum = Integer.parseInt(num1.charAt(i) +"") + Integer.parseInt(num2.charAt(i) +"")+ nTakeOver; nTakeOver = 0; System.out.println("maxLen:"+maxLen); if(i < (maxLen - 1)) { if(nSum >= 10) { nTakeOver = 1; result.append(nSum - 10); }else { nTakeOver = 0; result.append(nSum); } }else { StringBuffer lastSum = new StringBuffer(); //最後一位數的加法特殊處理,先反轉 lastSum.append(nSum).reverse(); result.append(lastSum); result.reverse().toString(); } } return result.toString(); } }