java求常数e

 代码演示

public static void main(String[] args) {
       double e= 1;
       int n =1;
       double item= 1;
       while (item>1e-6){
        e+= item;
        n++;
        item = 1.0/fact(n);
       }
       System.out.printf("e=%f",e);
   }
    /**
     * 计算阶乘
     * @param i
     * @return
     */
    private static double fact(double i) {
        double sum = 1;
        for (int m=1;m<=i;m++){
            sum *= m;

        }
        return sum;
    }

 结果输出

你可能感兴趣的:(java求常数e)