使用while循环计算1+1/2+1/3+1/4+...+1/20的和
public class Test {
public static void main(String[] args) {
int i = 1;
double sum = 0;
while (i<=20){
sum = sum+(double)1/i;
i++;
}
System.out.println("sum="+sum);
}
}
//运行结果sum=3.597739657143682