我的斐波那契 java 代码的循环不会启动

我需要为我的班级创建一个斐波那契项目,而我的循环不想开始


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

        }

    }

}

这是我的派生类,供那些询问每个类做了什么的人使用


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;

    }

}

希望这个问题不只是在我眼前而且我没有看到它。试图进入编码,所以这是一个开始。


慕虎7371278
浏览 92回答 2
2回答

海绵宝宝撒

你的斐波那契类看起来像这样吗?class FibonacciExample1{&nbsp;&nbsp;&nbsp; &nbsp; public static void main(String args[]){&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; int n1=0,n2=1,n3,i,count=10;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(n1+" "+n2);&nbsp; &nbsp; &nbsp; &nbsp; for(i=2;i<count;++i) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n3=n1+n2;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(" "+n3);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n1=n2;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n2=n3;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}&nbsp;

幕布斯7119047

如果我必须这样做,我会选择不同的方法 - 但这是你的程序,所以我只修复了你的错误:public class&nbsp; Fibonacci {&nbsp; &nbsp; private int n;&nbsp; &nbsp; private int fib1;&nbsp; &nbsp; private int fib2;&nbsp; &nbsp; private int fib3;&nbsp; &nbsp; public Fibonacci(int ny){&nbsp; &nbsp; &nbsp; &nbsp; n=ny;&nbsp; &nbsp; &nbsp; &nbsp; fib1=1;&nbsp; &nbsp; &nbsp; &nbsp; fib2=1;&nbsp; &nbsp; &nbsp; &nbsp; fib3=0;&nbsp; &nbsp; }&nbsp; &nbsp; public int nextNumber() {&nbsp; &nbsp; &nbsp; &nbsp; while(n-->2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fib3=fib1+fib2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fib1=fib2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fib2=fib3;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return fib3;&nbsp; &nbsp; }&nbsp; &nbsp; public int getFib1() {&nbsp; &nbsp; &nbsp; &nbsp; return fib1;&nbsp; &nbsp; }&nbsp; &nbsp; public int getFib2() {&nbsp; &nbsp; &nbsp; &nbsp; return fib2;&nbsp; &nbsp; }}public class FibonacciGenerator{&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Scanner sd = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter the amount of fibonaccis you want: ");&nbsp; &nbsp; &nbsp; &nbsp; int ny = sd.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; Fibonacci num = new Fibonacci(ny);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(num.getFib1());&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(num.getFib2());&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 3; i < ny; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num&nbsp; = new Fibonacci(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(num.nextNumber());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java