我最近完成了一個編程挑戰,挑戰我們獲得2015的階乘。在計算階乘之後,您將總和中的所有數字(在本例中爲BigInteger)進行求和。因子編程挑戰混淆
完成後,我取得的結果是:23517
然而,當我提交給了教授他說正確的結果是:26532
我似乎無法找到問題根本。我已經交叉檢查了多個來源,到目前爲止,一切都似乎表明我是對的。任何人都可以找到我的代碼出錯的地方?
import java.math.BigInteger;
class toymeister {
public static void main(String[] args){
System.out.println("The sum is: " + SumOf(FactorialOf(2015)));
}
public static BigInteger FactorialOf(int x){
BigInteger Answer, intConvert;
intConvert = new BigInteger("0");
Answer = new BigInteger("1");
for(int i=1; i<=x;i++){
intConvert = intConvert.valueOf(i);
Answer = Answer.multiply(intConvert);
}
System.out.println("The Factorial Of " + x + " is: " + Answer);
return Answer;
}
public static BigInteger SumOf(BigInteger y){
BigInteger Sum = new BigInteger("0");
while(y.compareTo(BigInteger.ZERO) > 0) {
Sum = Sum.add(y.mod(BigInteger.TEN));
y = y.divide(BigInteger.TEN);
}
return Sum;
}
}
首先,請按[Java命名約定(http://www.oracle.com/technetwork /java/codeconventions-135099.html):變量名稱應該以小寫字母開頭... – Frakcool
http://www.wolframalpha.com/input/?i=digitsum%28fact%282015%29%29因此23517是正確的 – 2015-11-19 18:27:35
打開你的調試器朋友。 – csmckelvey