【问题描述】由用户输入一个整数m(范围在1-15之间),
计算1!+2!+3!+…+m!。程序编译成功后,运行效果如下。
用了一个if判断输入数字的范围
用了for循环的嵌套
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int a = 1;
int sum = 0;
if (m >=15) {
System.out.println("The entered integer is not in the specified range.");
} else {
for (int c = 1; c <= m; c++) {
a = 1;
for (int b = 1; b <= c; b++) {
a = a * b;
}
sum = a + sum;
}
System.out.println("1!+2!+3!+...+"+m+"!"+"= "+sum);
}
}