//搞定
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入不超过10的正整数:");
while(true){
int n = sc.nextInt();
if(n<1||n>10){
System.out.println("输入不合法,请输入不超过10的正整数:");
}else{
int s = add(n);
System.out.println("1到"+n+"的阶乘的和为:"+s);
break;
}
}
}
//计算1~n的阶乘的和
public static int add(int n){
int sum = 0;
while(n>0){
sum= sum+fac(n);
n--;
}
return sum;
}
//计算n的阶乘
public static int fac(int num){
if(num==1){
return 1;
}else{
return num*fac(num-1);
}
}
}
//计算n的阶乘还可以用循环可以写成这样:
public static int test(int num){
int result = 1;
while(num>0){
result = result*num;
num--;
}
return result;
}
那个好懂用哪个