编写Java程序,求1+2/1+3/2+4/3+5/5+6/8+7/13+...前20项之和
时间: 2023-12-12 22:35:18 浏览: 83
Java求1+2!+3!+...+20!的和的代码
5星 · 资源好评率100%
以下是Java程序,求1+2/1+3/2+4/3+5/5+6/8+7/13+...前20项之和的代码:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n = 20;
double sum = 0.0;
double a = 1.0, b = 2.0;
for (int i = 1; i <= n; i++) {
sum += b / a;
double temp = b;
b = a + b;
a = temp;
}
System.out.printf("%.4f", sum);
}
}
```
输出结果为:32.6603
阅读全文