读书人

一个BigInteger的有关问题

发布时间: 2012-05-24 11:55:41 作者: rapoo

一个BigInteger的问题
import java.math.BigInteger;

public class FactorialCalculatorBigInteger {

public static BigInteger factorial( BigInteger number )
{
BigInteger result = BigInteger.ONE;

for( BigInteger i = number; i.compareTo( BigInteger.ONE ) != -1;
i.subtract( BigInteger.ONE ) )
{
result.multiply( i );
}

return result;
}

public static void main( String[] args )
{
for( int counter = 0; counter <= 3; counter++ )
{
System.out.printf( "%d! = %d\n", counter, factorial(BigInteger.valueOf(counter)) ) ;
}
}
}
这个哪里错了 怎么结果只是的
0!=1

[解决办法]

Java code
for( BigInteger i = number; i.compareTo( BigInteger.ONE ) != -1;              [color=#FF0000]i = i.subtract( BigInteger.ONE )[/color] )
[解决办法]
Java code
        for( BigInteger i = number; i.compareTo( BigInteger.ONE ) != -1;                 i = i.subtract(BigInteger.ONE)){            result = result.multiply(i);        } 

读书人网 >J2SE开发

热点推荐