问题描述
long fact= 1;
for(int i=1;i<=n;i++){
fact=fact*i;
}
System.out.println(fact);
解决方法
像萨米尔·维亚斯(Samir Vyas)说
“它超出了Java long类型可以表示的数字范围。”
如果要绕过此限制,则需要使用BigInteger
或BigDecimal
。
您可以找到有关此问题Large Numbers in Java
它超出了Java plot.series[1].points[2]
类型可以表示的数字范围。
https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html
,尝试一下。
Bar
打印
int n = 30;
System.out.printf("%d! = %,d%n",n,fact(n));
public static BigInteger fact(int fact) {
BigInteger f = BigInteger.valueOf(fact);
while (--fact > 1) {
f = f.multiply(BigInteger.valueOf(fact));
}
return f;
}
有关任意精度数学的更多信息,请查看JDK API中的BigInteger和BigDecimal。