public class TestNumber {
public static void main(String[] args) {
String number1 =       "335426587768665654444444444444444444445434";
String number2 = "33439985555555544444444444444444444445535426587768665655434";
char[] num1 = number1.toCharArray();
char[] num2 = number2.toCharArray();
boolean isLarge = false;
int length = num1.length > num2.length ? num2.length : num1.length;
int maxLength=num1.length > num2.length ? num1.length : num2.length;
char[] sChar= new char[maxLength];
for (int i = length-1 ; i >= 0; i--) {
int result = 0;
if (isLarge==true) {
result = (int) (num1[i] - '0') + (int) (num2[maxLength-length+i] - '0') + 1;
} else {
result = (int) (num1[i] - '0') + (int) (num2[maxLength-length+i] - '0');
}
if(result>=10){
result-=10;
isLarge=true;
}else{
isLarge=false;
}
            sChar[maxLength-length+i]=(char)(result+(int)'0');
}
for(int i=maxLength-length-1;i>=0;i--){
int result = 0;
if (isLarge) {
result = (int) (num2[i] - '0') + 1;
} else {
result =  (int) (num2[i] - '0');
}
if(result>=10){
result-=10;
isLarge=true;
}else{
isLarge=false;
}
            sChar[i]=(char)(result+(int)'0');
}
        for(int i=0;i<sChar.length;i++){
         System.out.print(sChar[i]);
        }
        System.out.println();
        System.out.println(sChar);
}
}

解决方案 »

  1.   

    String number1 = "335426587768665654444444444444444444445434";
    String number2 = "33439985555555544444444444444444444445535426587768665655434";
    BigInteger num1 = new BigInteger(number1);
    BigInteger num2 = new BigInteger(number2);
    BigInteger num3 = num1.add(num2);
    System.out.println(num3.toString());楼主太强了,BigInteger 也是采用类似的方法。
      

  2.   

    晕...直接面向加法编程不就行了...按位加class bit{  //每位的对象
     int i,j;//  0<=i<10 0<=i<10
     int flag;
    }觉得有点像刚做完的n位二进制加法器电路...晕
      

  3.   

    最简单当然是使用BigInteger,简单。如果一定自己写也不复杂
    String number1 =       "335426587768665654444444444444444444445434";
    String number2 = "33439985555555544444444444444444444445535426587768665655434";
    char[] buff = new char[Math.max(number1.length(), number2.length())];
    int carry = 0;
    for(int i = buff.length - 1; i >= 0 ; i--) {
    int num = carry; 
    int number1Index = number1.length() - (buff.length - i);
    int number2Index = number2.length() - (buff.length - i);
    if(number1Index >= 0)
    num += number1.charAt(number1Index) - '0';
    if(number2Index >= 0)
    num += number2.charAt(number2Index) - '0';
    if(num >= 10) {
    carry = 1;
    num -= 10;
    } else
    carry = 0;
    buff[i] =(char)('0' + num);
    }
    String result = new String(buff);
    if(carry > 0) {
    result = '1' + result;
    }
    System.out.println(result);