gpt4 book ai didi

java - 斐波那契数列,用 Java 写一行?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:37:14 24 4
gpt4 key购买 nike

<分区>

我想知道如何用一行代码以大多数 Javatic 方式找到第 n 个斐波那契数。这是我的代码,但我想学习更好的方法。

class FibonacciExample1 {
public static void main(String[] args) {
int n1 = 0, n2 = 1, n3, i, count = 10;
System.out.print(n1 + " " + n2);//printing 0 and 1

for (i = 2; i < count; ++i)//loop starts from 2 because 0 and 1 are already printed
{
n3 = n1 + n2;
System.out.print(" " + n3);
n1 = n2;
n2 = n3;
}

}
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com