私は、このメソッドを使用して数値の階乗をやろうとしている:JavaでBigIntegerメソッドを正しく使うには?
public static BigInteger factorial (long n) {
BigInteger result = BigInteger.ONE;
for(BigInteger i = BigInteger.TWO; i.compareTo(BigInteger.valueOf(n)) >= 0; i = i.add(BigInteger.ONE)) {
result = result.multiply(i);
}
return result;
}
とTE結果は常に1(BigInteger.ONE)のまま、なぜ私は聞いていますのよ?このオブジェクトが、未満に等しいか、または指定されたオブジェクトよりも大きいよう負の整数、ゼロ、または正の整数:
を使用し、間違っていますか? –
結果=結果* i; –
最終結果はnでなければなりません! –