跪求指导:求1--30阶乘之和,求高手看看我的程序错误在哪?
public class yueye4{
public static void main(String [] args){
long b=1,d=0;
for(long c=1;c<=3;c++){
for(long i=c;i>=1;i--)
{b=b*i;}
d+=b;
}
System.out.println("1!+2!+''''''+30!="+d);
}
}
------解决方案--------------------
用递归吧
public class TestTG {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(method(30));
}
public static long method(int i){
if(i==1){
return 1L;
}else{
return i*method(i-1);
}
}
}
------解决方案--------------------
Java code
long b = 1, d = 0;
for (long c = 1; c <= 3; c++) {
//每次外循环开始时 ,重新初始化b=1,开始下一个值c的阶乘计算
b = 1;
for (long i = c; i >= 1; i--) {
b = b * i;
}
d += b;
}
System.out.println("1!+2!+''''''+30!=" + d);