案例:计算1!+2!+3!+......+n!

时间:2021-09-04 03:58:50
/*
* 1!+2!+3!+......+n!
* */
import java.util.Scanner; public class ForTest{
public static void main(String args[]){
Scanner sc = new Scanner(System.in );
System.out.println("请输入N的值:");
int n = sc.nextInt();
int sum = 0;
for(int j = 1; j<=n; j++){
int num = 1;
for(int i = 1; i <= j; i++){
num *= i;
}
sum += num;
}
System.out.println("各项阶层的和为:" + sum);
}
}