Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        5年前关闭。
                                                                                            
                
        
我正在尝试编写一个生成“第n个”斐波那契数的Java程序。我究竟做错了什么?!

 public class project7 {

    public static void main(String[] args) {
        ConsoleReader console = new ConsoleReader(System.in);

        int fold1 = 1;
        int fold2 = 1;


        System.out.println("Enter the number of times you would like to compute:");
        int n = console.readInt();

        for(int i = 0; i <= n; i++ ){

            fold1++;
            fold2++;
        }

        int fnew = fold1 + fold2;
        System.out.println(fnew);

    }
}

最佳答案

除了数字以外,您还期望什么?
因此,首先您初始化控制台读取器以获取用户输入。
然后将2个Integer初始化为值1 ... fold1和fold2 ...然后获得int n的输入值...所有这些都很棒。然后,使n + 1个循环递增fold1和fold2 ...好的..然后将它们添加到fnew中并打印结果...这就是此代码的作用。假设您得到5的输入... fold1将以1开始递增6次,所以在六个循环中(由于
更新:

尝试这个
    int fold1 = 0;
    int fold2 = 1;
    int n = console.readInt ...

for(int i=0; i<n;i++)
{
Fnew = fold1 + fold2;
fold1 = fold2;
fold2 = Fnew;


}
System.out.println(Fnew);

10-04 18:03