0
import java.util.Scanner;

public class Fibonacci
{
    public static void main(String[] args)
    {
        int count;
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter number");

        count = in.nextInt();

        int[] fib = new int [count];
        fib[0] = 1;
        fib[1] = 1;

        for (int i=2; i<count; i++)
        {
            fib[i] = fib[i-1] + fib[i-2];
        }

        for(int i=0; i<count; i++)
        {
            System.out.print(fib[i] + " ");

        }
    }
}

这是我非常简单的 Fib 程序,我不明白为什么它总是少一个数字。例如:

运行:请输入数字 6 1 1 2 3 5 8 BUILD SUCCESSFUL(总时间:5 秒)

运行:请输入数字 7 1 1 2 3 5 8 13 BUILD SUCCESSFUL(总时间:5 秒)

我认为在我的 FOR 循环中应该是 "(int i=2; i <= count;"

但是当我在两者中都输入大于或等于时,或者在任何一个 FOR 循环中都会给我一个错误

有什么建议么?我知道这很容易我忽略了

4

5 回答 5

1

数组是从零开始的。这意味着,(假设count = 5)如果您有以下数组:

int[] fib = new int[5];

然后您可以访问fib[0]fib[1]fib[2]和。所以fib[3]fib[4]

for (int i = 0; i < 5; i++) {
    System.out.print(fib[i] + " ");
}

会好的。因为它将访问 中的所有内容fib,从索引 0 开始,并以小于 5 的最后一个索引(即 4)停止。但是,如果您这样做:

for (int i = 0; i <= 5; i++) {
    System.out.print(fib[i] + " ");
}

那么您将访问小于OR EQUAL TO 5 的最后一个索引,即 5。但是,如前所述,fib[5]它是无效的。这就是给你错误的原因。

于 2013-09-10T01:57:19.747 回答
1

您的代码给出了正确的输出。但是如果您还需要一个元素,请尝试使用 count + 1 初始化数组,然后让您的循环运行 for i <= count

public static void main(String[] args) {


int count;
Scanner in = new Scanner(System.in);
System.out.println("Please enter number");

count = in.nextInt();

        int[] fib = new int [count+1];
        fib[0] = 1;
        fib[1] = 1;

        for (int i=2; i <= count; i++){
            fib[i] = fib[i-1] + fib[i-2];
        }

         for(int i=0; i <= count; i++){
             System.out.print(fib[i] + " ");

         }
            }
        }
于 2013-09-10T01:43:33.143 回答
1

一个更简单的解决方案是首先避免需要一个数组,并且您不需要获得正确的大小。

public static void main(String[] args) {
    System.out.println("Please enter a number");
    Scanner in = new Scanner(System.in);
    int count = in.nextInt();

    long a = 1, b = 1;
    for(int i = 0; i < count; i++) {
        System.out.print(a + " ");
        long c = a + b;
        a = b;
        b = c;
    }
    System.out.println();
}
于 2013-09-10T06:35:41.023 回答
0
public class Fibonacci
{
 private int [] fibArray;

public Fibonacci()
{
}

public void Fibonacci()
{
    fibArray = new int[0];
}

public void setFibonnaci(int size)
{
    fibArray = new int[size];

    if(fibArray.length == 1)
    {
        fibArray [0] = 0;
    }

    else if(fibArray.length == 2)
    {
        fibArray[0] = 0;
        fibArray[1] = 1;
        fibArray[2] = 2;
    }
    else 
    {
        fibArray[1] = 1;
        fibArray[0] = 0;

        for(int x = 2; x < fibArray.length; x++)
        {
            fibArray [x] = fibArray[x-1] + fibArray[x-2];
        }
    } 
}

public int getSequence(int number)
{
    if(number -1 < fibArray.length)
    {
        return fibArray[number - 1];
    }
    return -1;
}

//check the test case for getFibo
public String toString()
{
    String output = "";
    for (int x = 0; x < fibArray.length; x++)
    {
        output += x + " - " + fibArray[x];
    }
    return output;
}

}

响应较晚,但对站点不熟悉,只是想提供帮助。这个 fib 类 100% 有效

于 2014-02-06T01:44:21.323 回答
0

int fib[] 应该还有一个数组元素空间,因此 fib[count] 可以被存储。

import java.util.Scanner;

public class Fibonacci
{
    public static void main(String[] args)
    {
        int count;
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter number");

        count = in.nextInt();

        int[] fib = new int [count + 1];
        fib[0] = 1;
        fib[1] = 1;

        for (int i=2; i <= count; i++)
        {
            fib[i] = fib[i-1] + fib[i-2];
        }

        for(int i = 0; i<= count; i++)
        {
            System.out.print(fib[i] + " ");

        }
    }
}
于 2013-09-10T01:53:29.327 回答