0

Okay my new question is how I can let the array print out all numbers of the segment. At the moment I can input a number and the code will print out the corresponding value in Fibonacci. However, I would like the array to print out all values leading up to the answer. Ex. Input = 7, array prints out 0, 1, 1, 2, 3, 5, 8 instead of just 8

package math;
public class Fibonacci {
    public static long getFib(int n) {
        long Fibby[] = new long[n+1];
        Fibby[0] = 1;
        Fibby[1] = 1;       
    for(int i = 2; i<=n; i++) {    //initialize loop        
        Fibby[i] = Fibby[i-1] + Fibby[i-2];             
    } // end of for loop
    return Fibby[n];   //end method getfib    
    }
}

And the runner

package math;


         Scanner key = new Scanner(System.in);



        Fibonacci f = new Fibonacci();
        int p;
        System.out.println("Fib value : ");
        p = key.nextInt();

        System.out.println( "Fib Value of "+ p +" :: " + f.getFib(p) );



    }   

How can this happen? My question has been downsized.

king
  • 11
  • 6

2 Answers2

2

You can't run your main method, because System.out.println() expects a parameter it can print. However, your fib() method returns void, so there is nothing to print. Add a return type to your fib() method, and your error in main() will be resolved. Here's a demonstration of printing the 0th to 12th Fibonacci numbers:

FibonacciRunner.java

public class FibonacciRunner
{
    public static void main(String[] args)
    {
        for(int i = 0; i <= 12; i++)
        {
            System.out.println(Fibonacci.fib(i));
        }
        for(int i = 0; i <= 12; i++)
        {
            System.out.println(Fibonacci.fibList(i));
        }
    }
}

Fibonacci.java

public class Fibonacci
{
    public static long fib(int n)
    {
        long current = 0;
        long next = 1;
        for(int i = 0; i < n/2; i++)
        {
            current += next;
            next += current;
        }
        return n % 2 == 0 ? current : next;
    }
    public static List<Long> fibList(int n)
    {
        List<Long> ret = new ArrayList<>(n == 0 ? List.of(0L) : List.of(0L, 1L));
        long current = 0;
        long next = 1;
        for(int i = 0; i < n/2; i++)
        {
            current += next;
            next += current;
            if(i*2+1 <= n)
                ret.add(current);
            if(i*2+2 < n)
                ret.add(next);
        }
        return ret;
    }
}

Output:

0
1
1
2
3
5
8
13
21
34
55
89
144
[0]
[0, 1]
[0, 1, 1]
[0, 1, 1, 2]
[0, 1, 1, 2, 3]
[0, 1, 1, 2, 3, 5]
[0, 1, 1, 2, 3, 5, 8]
[0, 1, 1, 2, 3, 5, 8, 13]
[0, 1, 1, 2, 3, 5, 8, 13, 21]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
Avi
  • 2,611
  • 1
  • 14
  • 26
0

One problem with your code is that Fibonacci.fib doesn't return anything, so what are you expecting the FibonacciRunner to print?

Another is that arrays in Java have fixed length. Consider using a List instead:

List fibby = new ArrayList();
fibby.add(0);
fibby.add(1);
for (int i = 2; i < n; i++){
   fibby.add(fibby.get(i - 1) + fibby.get(i - 2));
}
Simon Crane
  • 2,122
  • 2
  • 10
  • 21
  • 1
    Making a list is a spectacularly bad idea for calculating the nth Fibonacci number, and a great idea for calculating the 1-to-nth Fibonacci numbers. As it is now, it appears that he wants "the output of the place of that sequence part", which probably indicates the nth Fibonacci number. They only need two variables to calculate the nth Fibonacci number, not a List. – Avi Sep 26 '19 at 16:47