- e=1+ 1/1!+1/2!+1/3!+1/4!+…+1/n!
public class Test { public static void main(String args[]) throws Exception { // 方法1 double e = 1.0; double t = 1.0; for (int i = 1; i <= 20; i++) { t /= i; e += t; } System.out.println(e); // 方法2,不太理解,等有时间再补坑 e = 0.0; for (int i = 20; i > 0; i--) { e = (e + 1.0) / i; } e++; System.out.println(e); } }其中
t/=i;是计算阶乘的好方法