小弟刚刚接触java有很多不明白的地方,下面是我编的程序,实现1!+2!+...+n!不知道哪里出问题了:
package Package8;
import java.util.Scanner;
public class MainClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Suanfa suanfa=new Suanfa();
Scanner reader=new Scanner(System.in);
System.out.println("请输入基数n:");
double n=reader.nextDouble();
suanfa.giveSum(n);
}
}
package Package8;
public class Suanfa {
void giveSum(double n){
double i,j,jiecheng=1,sum=0;
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
jiecheng=jiecheng*j;
}
sum=sum+jiecheng;
}
System.out.println("阶乘的和为:"+sum);
}
}
为什么执行后n=3的时候会是15呢? |
|