java中用while循环计算1+1/2!+1/3!+...+1/20!之和
java中用while循环计算1+1/2!+1/3!+...+1/20!之和
日期:2017-08-20 05:49:55 人气:1
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);
}
}