java菲波那切数列
时间: 2023-11-12 11:57:59 浏览: 92
cpp代码-菲波那切数列
Java实现菲波那切数列可以使用递归或循环的方式,以下是循环实现的示例代码:
```java
public static int fibonacci(int n) {
if (n <= 1) {
return n;
}
int prev = 0, curr = 1;
for (int i = 2; i <= n; i++) {
int next = prev + curr;
prev = curr;
curr = next;
}
return curr;
}
```
阅读全文