Fibonnaci.java和跑步者

时间:2016-11-03 23:09:44

标签: java arrays fibonacci

我在完成这项任务时遇到了麻烦,可以使用一些帮助以下是包含示例输入和输出的说明:

enter image description here

https://i.stack.imgur.com/nawNh.png。任何帮助将不胜感激。

import java.util.Arrays;

public class FibonacciRunner
{

    public static void main(String[] args) {
        int[] fibArray = {1,2,3,4,5,6,11,16,21,31,41,46,1,1,2,1,2,11};
        Fibonacci fibo = new Fibonacci();
        System.out.println(fibo.getFibo());
        System.out.println(fibo.getFibo());
        System.out.println(fibo.getFibo());
        System.out.println(fibo.getFibo());
        System.out.println(fibo.getFibo());
        System.out.println(fibo.getFibo());
        System.out.println(fibo.getFibo());
        System.out.println(fibo.getFibo());
        System.out.println(fibo.getFibo());
        System.out.println(fibo.getFibo());
        System.out.println(fibo.getFibo());
        System.out.println(fibo.getFibo());
        System.out.println(fibo.getFibo());
        System.out.println(fibo.getFibo());
        System.out.println(fibo.getFibo());

    }
}
public class Fibonacci
{
    int[] fibArray;
    public Fibonacci()
    {
        fibArray = new int[50];
        this.setFibo();
    }
    public void setFibo()
    {
        fibArray[0]=1;
        fibArray[1]=1;
        for(int idx = 2; idx<fibArray.length; idx++){
            fibArray[idx] = fibArray[idx-1] + fibArray[idx-2];
        }

    }
    public int getFibo(int index)
    {
    if(index < 50)
     return fibArray[index];
    else
      return -1;
    }

    public String toString()
    {
        return this.getFibo() + "\n";
    }
}

1 个答案:

答案 0 :(得分:0)

阿什利是对的。你应该在主程序中做这样的事情:

for (int i = 0; i < fibArray.length; i++ )
    System.out.println(fibo.getFibo(fibArray[i]));