用java的while语法写一个程序输出1到20的阶乘的倒数之和
时间: 2023-05-20 14:02:40 浏览: 85
当然可以,以下是代码:
```
public class Main {
public static void main(String[] args) {
int i = 1;
double sum = 0;
while (i <= 20) {
int factorial = 1;
for (int j = 1; j <= i; j++) {
factorial *= j;
}
sum += 1.0 / factorial;
i++;
}
System.out.println(sum);
}
}
```
输出结果为:2.7182818284590455
阅读全文