java编写应用程序1!+ 2!+ ......+10!并输出结果
时间: 2023-11-11 19:06:20 浏览: 99
好的,我将为您编写一个Java程序来计算并输出1!到10!的结果。以下是代码示例:
```java
public class Factorial {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
long factorial = calculateFactorial(i);
System.out.println(i + "! = " + factorial);
}
}
private static long calculateFactorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * calculateFactorial(n - 1);
}
}
```
运行以上代码,将会输出1!到10!的结果:
```
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
```
阅读全文