1 public static void main(String[] args) { 2 double n = 1, sum = 0; 3 while (n <= 20) { 4 sum += 1 / Factorial(n); 5 n++; 6 } 7 System.out.println(sum); 8 9 } 10 11 static double Factorial(double m) { 12 if (m == 1 || m == 0) { 13 return 1; 14 } else { 15 return (m * Factorial(m - 1)); 16 } 17 }
1 public static void main(String[] args) { 2 3 double n = 1, j = 1, sum = 0; 4 while(n <= 20){ 5 j = j*n; 6 sum += 1/j; 7 n++; 8 } 9 System.out.println(sum); 10 }
时间: 2024-10-05 06:19:08