Java输出斐波那契数列前20项之和
时间: 2023-11-18 07:04:57 浏览: 150
计算斐波那契数列前20项
好的,您可以使用以下代码输出斐波那契数列前20项之和:
```java
public class FibonacciSum {
public static void main(String[] args) {
int n = 20;
int sum = 0;
int[] fib = new int[n];
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i < n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
for (int i = 0; i < n; i++) {
sum += fib[i];
}
System.out.println("斐波那契数列前20项之和为: " + sum);
}
}
```
阅读全文