用e ≈1+1/1!+1/2!+1/3!+……求e的近似值,要求误差小于0.00001。

用e ≈1+1/1!+1/2!+1/3!+……求e的近似值,要求误差小于0.00001。

public class jieda {
  public static void main(String[] args) {
      double e=1,sum=1;    //e的初值为1,sum用来存放n!
      int i=1;
      while(Math.abs(e-Math.E)>Math.pow(10,-5)){  //要求误差小于0.00001
      sum=i*sum;                                                        
      e=1.0/sum+e;         
      i++;
      }
      System.out.println("e="+e);
   }
}

你可能感兴趣的:(Java)