BigInteger计算
样例输入:123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
样例输出:370370367037037036703703703670
这是我的答案:
package com.example;
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BigInteger bi = new BigInteger("0");
while (in.hasNext()) {
String str = in.next();
if (str.equals("0")) {
break;
}
bi.add(new BigInteger(str));
}
in.close();
System.out.println(bi);
}
}
这是标准答案:
import java.math.BigInteger;
import java.util.Scanner;
public class Integer_Inquiry {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BigInteger sum=new BigInteger("0");
while(in.hasNext()){
String str=in.next();
if(str.equals("0")){
System.out.println(sum);
sum=new BigInteger("0");
}
BigInteger n=new BigInteger(str);
sum=sum.add(n);
}
in.close();
}
}
我的代码哪里出错了?为什么结果总是0?