j ava斐波那契数列前40
时间: 2023-10-19 17:05:47 浏览: 84
输出fibonacci数列的前40项
下面是 Java 实现的斐波那契数列前40项:
```java
public class FibonacciSequence {
public static void main(String[] args) {
int n = 40;
int[] fibonacci = new int[n];
fibonacci[0] = 0;
fibonacci[1] = 1;
for (int i = 2; i < n; i++) {
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
}
for (int i = 0; i < n; i++) {
System.out.print(fibonacci[i] + " ");
}
}
}
```
输出结果:
```
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155
```
阅读全文