-2

I need to create a Fibonacci project for my class and my loop doesn't want to start

import java.util.Scanner;

public class FibonacciGenerator{
    public static void main(String[] args){
        Scanner sd = new Scanner(System.in);
        System.out.println("Enter the amount of fibonaccis you want: ");
        int ny = sd.nextInt();
        Fibonacci num = new Fibonacci(ny);

        System.out.println(num.getFib1());
        System.out.println(num.getFib2());

        for(int i = 1; i < ny; i++){
            System.out.println(num.nextNumber());
            num  = new Fibonacci(ny);
        }
    }
}

Here's my derived class for those asking for what each class did

public class Fibonacci {
    private int n;
    private int fib1;
    private int fib2;
    private int fib3;

    public Fibonacci(int ny){
        n=ny;
        fib1=1;
        fib2=1;
        fib3=0;
    }
    public int nextNumber() {
        while(n>2) {
            fib3=fib1+fib2;
            fib1=fib2;
            fib2=fib3;
        }
        return fib3;
    }
    public int getFib1() {
        return fib1;
    }
    public int getFib2() {
        return fib2;
    }
}

hopefully, the issue isn't just right in front of my eyes and I don't see it. Trying to get into coding so this is a start.

Lakshan Dissanayake
  • 521
  • 1
  • 4
  • 18
Nickolas E
  • 23
  • 4
  • 1
    Have you tried stepping through the code? – Steve Smith Feb 06 '19 at 17:02
  • Your loop doesn't "want" anything. It doesn't have a will of its own. What your program *does* do is execute exactly what logic it's written to do. Sounds like now is the time for you to familiarize yourself with debugging. This is often a good place to start: https://ericlippert.com/2014/03/05/how-to-debug-small-programs – David Feb 06 '19 at 17:04
  • 1
    You have not provided Fibonacci class – jnr Feb 06 '19 at 17:05
  • consider adding some information about classes you have used in your code – PranshuKhandal Feb 06 '19 at 17:05
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – EJoshuaS - Stand with Ukraine Feb 06 '19 at 17:05
  • and you are re-assigning variable num in each iteration – PranshuKhandal Feb 06 '19 at 17:06
  • Debugging with println() is quite good, why not. You might consider specifying how it should behave before doing actual desing/coding, like this: $java com.Fibinacci Enter the amount of fibonaccis you want: 10 Fibonacci numbers are: 1 1 2 3 5 ... ... – jnr Feb 06 '19 at 17:08

2 Answers2

1

Does your Fibonacci class look like this?

class FibonacciExample1{  
    public static void main(String args[]){    
        int n1=0,n2=1,n3,i,count=10;    
        System.out.print(n1+" "+n2);

        for(i=2;i<count;++i) {    
            n3=n1+n2;    
            System.out.print(" "+n3);    
            n1=n2;    
            n2=n3;    
        }
    }
} 

example taken from here

Lakshan Dissanayake
  • 521
  • 1
  • 4
  • 18
0

If i had to do it I'd choose a different aproach - but it's your program, so I only fixed your errors:

public class  Fibonacci {
    private int n;
    private int fib1;
    private int fib2;
    private int fib3;

    public Fibonacci(int ny){
        n=ny;
        fib1=1;
        fib2=1;
        fib3=0;
    }
    public int nextNumber() {
        while(n-->2) {
            fib3=fib1+fib2;
            fib1=fib2;
            fib2=fib3;
        }
        return fib3;
    }
    public int getFib1() {
        return fib1;
    }
    public int getFib2() {
        return fib2;
    }
}
public class FibonacciGenerator{
    public static void main(String[] args) {
        Scanner sd = new Scanner(System.in);
        System.out.println("Enter the amount of fibonaccis you want: ");
        int ny = sd.nextInt();
        Fibonacci num = new Fibonacci(ny);

        System.out.println(num.getFib1());
        System.out.println(num.getFib2());

        for(int i = 3; i < ny; i++){
            num  = new Fibonacci(i);
            System.out.println(num.nextNumber());
        }
    }
}
kai
  • 894
  • 1
  • 7
  • 15