当前位置: 代码迷 >> Java相关 >> 跪求指导:求1-30阶乘之和,求高手看看小弟我的程序异常在哪
  详细解决方案

跪求指导:求1-30阶乘之和,求高手看看小弟我的程序异常在哪

热度:9055   发布时间:2013-02-25 21:44:54.0
跪求指导:求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);
  相关解决方案