package four;
public class fouronetwo {
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);
}
}
解释:当i=1, sum=1, i=2, a=1*(1/2);
当i=2, sum=1+1*1/2, i=3, a=1*1/2*(1/3);
当i=3, sum=1+1*1/2+1*1/2*1/3, i=4, a=1*1/2*1/3*(1/4)...
当i=20, sum=1+1*1/2+1*1/2*1/3+...+1*1/2*...*1/20, i=21, a=1*1/2*...*(1/21)
当i=21不成立