为什么出现负数,是因为你定义的数据类型长度不够导致的。
java 中 int 长度 2^-128 ~ 2^12750! = 3.0414093201713 * 10 64代码如下:package test;public class Factorial { public static void main(String[] args) {
// TODO Auto-generated method stub
double fac = 1;
int count = 50; for (int i = 1; i <= count; i++) {
fac = fac * i;
} System.out.println("1!+2!+.... +50!'s factorial is :" + fac);
}
}