使用while循环语句计算1+1/2!+1/3!+······1/20!之和。
  分母是阶乘
public class Example_02 {
  public static void main(String args[])
{
      double sum = 0,a = 1;
      int i = 1;
      while(i <= 20)
      {
       sum = sum+a;
       i = i+1;
       a = a*(1.0/i);
      }
      System.out.println(sum);
    }
}
这个没明白,请大神详细说明。
从头详细讲解,本人初学者。谢谢了

解决方案 »

  1.   


    public class Example_02 { 
       public static void main(String args[])
       { 
           double sum = 0,a = 1;
           int i = 1;
           while(i <= 20)    //从1/1!加到1/20!
           {
            sum = sum+a;    //sum保存连加的结果
            i = i+1;
            a = a*(1.0/i);    //1/n!=(1/(n-1)!)*(1/n)     
           }
           System.out.println(sum);
         }
     }
      

  2.   

    1/n!=(1/(n-1)!)*(1/n)
    比如1/2! = 1/1!*1/2
       1/3! = 1/2!*1/3
    你自己按循环写写就看出来了 别偷懒
      

  3.   


     a = a*(1.0/i);    阶乘体现在这,a,i随着循环在变,比如一开始1=1*(1.0/1),然后i加1了,就是a*(1.0/2)也就是a= 1*(1.0/2)也就是1/2!下面类推,希望LZ能看懂,刚形如都这样,加油...