用Java的递归方法求1!+2!+3!+...+10!的和
时间: 2023-07-08 08:46:41 浏览: 76
Java求1+2!+3!+...+20!的和的代码
5星 · 资源好评率100%
好的,我可以为您提供Java代码,如下所示:
```java
public class FactorialSum {
public static void main(String[] args) {
int n = 10;
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += factorial(i);
}
System.out.println("The sum of factorials from 1 to 10 is: " + sum);
}
public static int factorial(int n) {
if (n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
}
```
运行结果为:
```
The sum of factorials from 1 to 10 is: 4037913
```
希望能够帮助您!
阅读全文