java编写应用程序求1!+2!+...+10!。
时间: 2023-10-10 21:07:29 浏览: 219
您可以使用循环来计算1!到10!的结果。下面是使用Java编写的示例代码:
```java
public class Factorial {
public static void main(String[] args) {
// 计算1!到10!
for (int i = 1; i <= 10; i++) {
long factorial = getFactorial(i);
System.out.println(i + "! = " + factorial);
}
}
// 计算阶乘
public static long getFactorial(int n) {
long factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
return factorial;
}
}
```
输出结果为:
```
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
```
阅读全文