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.

6
  • What is package math supposed to do? All the code you're showing is base java. Also, what are radius and height? (your code doesn't use them, Fibbonacci numbers certainly don't use them, why are they in the method signature and call?) Commented Sep 26, 2019 at 16:39
  • What do you mean runner? Runnable? Commented Sep 26, 2019 at 16:40
  • from the code, it looks like they mean "main class that becomes the executable" Commented Sep 26, 2019 at 16:40
  • "I have complete errors" If you have errors you should post them here. Commented Sep 26, 2019 at 16:40
  • Oh ok. Runner = Main Class Commented Sep 26, 2019 at 16:41

2 Answers 2

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]
Sign up to request clarification or add additional context in comments.

Comments

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));
}

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.