使用while循环语句计算1+1/2!+1/3!+1/4!+...的前20项和
package myJava;
public class Example3_6 {
public static void main(String[] args) {
double sum = 0,item = 1;
int i = 1,n = 20;
while(i<=n) {
sum = sum+item;
i = i+1;
item = item*(1.0/i);
}
System.out.println("sum="+sum);
}
}
运行结果:sum=1.7182818284590455