【发布时间】:2014-06-23 10:47:39
【问题描述】:
这是真正的问题:
编写一个交互式程序,将两个整数相加,每个整数最多 50 位 (将整数表示为数字数组)。
这是一道作业题,使用的语言是 Java。我已经走了这么远,但我认为它甚至还没有接近。
1. 输入不超过 20 位,但必须使用 50 位。
2. 'integerToDigits' 方法正在生成两个数组,但我无法弄清楚如何使用它们并将它们添加到 main 方法中。
请帮忙。
package One;
import java.util.Scanner;
public class AddInt {
public static void main(String[] args) {
Long x,y;
Long a[] = new Long[50];
Long b[] = new Long[50];
System.out.println("Please enter two numbers which have no more than 50 digits: ");
Scanner s = new Scanner(System.in);
x = s.nextLong();
y = s.nextLong();
System.out.println(x+ "and "+y);
integerToDigits(x);
integerToDigits(y);
}
public static Long[] integerToDigits(Long n){
Long digits[] = new Long[50];
Long temp = n;
for(int i = 0; i < 50; i++){
digits[49-i] = temp % 10;
temp /= 10;
}
return digits;
}
}
【问题讨论】:
-
请更具体。您的问题非常模糊,将导致关闭它。
-
你不能用String然后转换成BigInteger来处理吗???
-
@StephenFrancis 你试过
tempBigInteger = tempBigInteger.divide(BigInteger.TEN)吗? -
@StephenFrancis
digits[49-i] = tempBigInteger.mod(BigInteger.TEN).longValue();? -
我认为作业应该自己尝试。不是众包的。 ;-)