class test2{
public static void main(String[] args){
String a = "8888899999999888";
String b ="88888888888888";
String str = new BigInteger(a).add(new BigInteger(b)).toString();
System.out.println(str);
}
}
import java.util.Map;
//StringBuffer 有.append方法
class test{
public static void main(String[] args){
String str1 = "19";
String str2 = "2";
System.out.println(add(str1,str2));
}
private static String add(String str1,String str2){
if(str1==null||"".equals(str1)){
return str2;
}
if(str2==null||"".equals(str2)){
return str1;
}
int maxlength = Math.max(str1.length(),str2.length());
StringBuffer result = new StringBuffer(maxlength+1);
//翻转两个字符串
str1 = new StringBuffer(str1).reverse().toString();
str2 = new StringBuffer(str2).reverse().toString();
System.out.println(str1);
System.out.println(str2);
int carry=0;
int currentNum = 0;
int minlength = Math.min(str1.length(),str2.length());
int i;
for(i = 0;i<minlength;i++){ //只能小于minlength 不然会报错
currentNum = str1.charAt(i)+str2.charAt(i) - 2*'0' +carry;
System.out.println(currentNum);
carry =currentNum/10;
System.out.println(carry);
currentNum %=10;
System.out.println(currentNum);
result.append(String.valueOf(currentNum));
}
if(str1.length()<str2.length()){
str1 = str2; //把那个长的付给str1
}
for(;i<str1.length();i++){
currentNum = str1.charAt(i)-'0'+carry;
carry =currentNum/10;
currentNum %=10;
result.append(String.valueOf(currentNum));
}
if(carry>0){
result.append(String.valueOf(carry)); //防止str1剩下的几位全是9
}
// for(;i<str1.length();i++){
//
// }
return result.reverse().toString();
}
}