public static int sumFactorial(int n) { //求1!+2!+3!+4!+........+n!的和 int sum = 0; for (int i = 1; i <= n; i++) { sum += factorial(i); } return sum; } public static int factorial(int n) { //求n! if (n == 1) { return 1; } return n*factorial(n-1); } public static void main(String[] args) { System.out.println(sumFactorial(3)); }
输出结果: