我试图将 2 个给定数字之间除边界之外的所有数字相加。例如,addNumbers("5", "8")由于 6+7=13,因此应该返回 13。这是我目前拥有的功能。
public static BigInteger addNumbers(String from, String to) {
BigInteger total = new BigInteger("0");
BigInteger startingBoundary = new BigInteger(from);
BigInteger finishingBoundary = new BigInteger(to);
if (startingBoundary.compareTo(finishingBoundary) < 0) {
startingBoundary = new BigInteger(from);
finishingBoundary = new BigInteger(to);
} else {
finishingBoundary = new BigInteger(from);
startingBoundary = new BigInteger(to);
}
while (startingBoundary.compareTo(finishingBoundary) != 0 ) {
System.out.println("Starting boundary:" + startingBoundary.intValue());
System.out.println("Finishing boundary: " + finishingBoundary.intValue());
total.add(startingBoundary);
System.out.println("total: "+total.intValue());
startingBoundary.add(new BigInteger("1"));
}
return total;
}
问题是,尽管我改变了 while 条件的值,但它似乎无限运行。另外,当打印出每个循环中的总对象时,它总是打印出 0。我知道我将其初始化为 0,但我希望它在添加时会发生变化。
GCT1015
慕丝7291255
qq_花开花谢_0
相关分类