用java写有一分数序列2/1,3/2,5/3,8/5,13/8,21/13求出这个数列的前20项之和
时间: 2023-12-13 12:48:19 浏览: 76
DNA.zip_数值算法/人工智能_Java_
代码如下:
```
public class FractionSequence {
public static void main(String[] args) {
int n = 20;
double sum = 0;
double a = 2, b = 1, c;
for (int i = 1; i <= n; i++) {
sum += a / b;
c = a + b;
b = a;
a = c;
}
System.out.println("前" + n + "项之和为:" + sum);
}
}
```
运行结果如下:
```
前20项之和为:32.66026079864164
```
阅读全文