对于这个题,我只会两个解法,希望可以帮到你
方法一:public class Fibonacci1{
//定义三个变量方法
public static void main(String[] args) {
int a=1, b=1, c=0;
System.out.println("斐波那契数列前20项为:");
System.out.print(a + "\t" + b + "\t");
for (int i = 1; i <= 18; i++) {
c = a + b;
a = b;
b = c;
System.out.print(c + "\t");
if ((i + 2) % 5 == 0)
System.out.println(); }}}
方法二:public class Fibonacci2{
//定义数组方法
public static void main(String[] args) {
int arr[] = new int[20];
arr[0] = arr[1] = 1;
for (int i = 2; i < arr.length; i++) {
arr[i] = arr[i - 1] + arr[i - 2]; }
System.out.println("斐波那契数列的前20项如下所示:");
for (int i = 0; i < arr.length; i++) {
if (i % 5 == 0)
System.out.println();
System.out.print(arr[i]+"\t"); }}} |