开发者

How Can I write a Fibonacci sequence, which uses an array?

Here is the code:

class Fibonacci {
    static final int MIN_INDEX = 1;
    public static void main (String[] args){
        int high = 1;
        int low = 1;
        String jel;
        System.out.println("9: " + high);

      for (int i = 8; i >= MIN_INDEX; i--){
        if (high % 2 == 0)
     开发者_运维问答     jel = " *";
        else 
          jel = " ";
        System.out.println(i + ": " + high + jel);
        high = low + high;
        low = high - low;
      }
   }
}

I want to write this program, to store the Fibonacci sequence in an array, and then write out them. But I can't write it. What can I do? I don't need to "mark them" with an *.


Here are the steps

  1. Calculate the Fibonacci Sequence numbers that you need; store each value.
  2. Print the values that you calculated.

Calculate and store the Fibonacci Numbers

The Fibonacci sequence is a recursive function, but it would be ridiculous to implement it in any probramming language using recursion. Instead use a loop. For example:

int[] fibonacciNumbers = new int[20];
fibonacciNumbers[0] = 0;
fibonacciNumbers[1] = 1;

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

Print the array

Loop through each element in the array and print it however you want; for example, you could System.out.println(fibonacciNumbers[index]);


I think this will answer your question (I also added handling for some pessimistic scenarios):

public class Fibonacci
{
    public static void main(String[] args)
    {
        int[] fibMembers = buildFibArray(9);
        printFibArray(fibMembers);
    }

    private static void printFibArray(int[] fibMembers)
    {
        for (int i = 0; i < fibMembers.length; i++)
        {
            System.out.print(fibMembers.length-i);
            System.out.print(": ");
            System.out.print(fibMembers[i]);
            if (fibMembers[i] % 2 == 0)
            {
                System.out.print(" *");
            }
            System.out.println("");
        }
    }

    private static int[] buildFibArray(int maxIndex)
    {
        int[] fibMembers = new int[maxIndex];

        if (maxIndex > 0)
        {
            fibMembers[0] = 1;
            if (maxIndex > 1)
            {
                fibMembers[1] = 1;

                for (int i = 2; i < fibMembers.length; i++)
                {
                    fibMembers[i] = fibMembers[i-2] + fibMembers[i-1];
                }
            }
        }
        return fibMembers;
    }
}

The buildFibArray method builds the array.
The printFibArray method prints the array - should be according to your requirements.


If you want only pair Fibonacci numbers :

public class Fibonacci {       

    private int count = 0;

    public Integer next() {
        return fib(count++);
    }

    private int fib(int n) {
        if (n < 2) {
            return 1;
        }
        return fib(n - 2) + fib(n - 1);
    }

public static void main(String[] args) {
    List<Integer> pairFib = new ArrayList<Integer>();
    Fibonacci gen = new Fibonacci();
    for (int i = 0; i < 18; i++) {
        int current = gen.next();
        if (current % 2 == 0) {
            pairFib.add(current);
        }
    }
    System.out.println(pairFib);
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜