简体   繁体   English

斐波那契数列除了用户输入的数字外不会打印任何内容

[英]Fibonacci sequence will not print anything but the number that is inputted by the user

Here is my main method, I am trying to call the Fibonacci sequence to tell me what number would be at the location the user inputs:这是我的主要方法,我试图调用斐波那契数列来告诉我用户输入位置的数字是多少:

import java.util.Scanner; //import Scanner

public class Main {
    public static void main(String[] args) {
         
       System.out.println("enter number");
       Scanner input = new Scanner(System.in); 
       int n = input.nextInt();
         
       Fibonacci fibonacci_test = new Fibonacci();
       fibonacci_test.Recursivefibonacci(n);
         
    }
}

Here is my Fibonacci code that I have:这是我的斐波那契代码:

public class Fibonacci {
//Fn=F(n-1)+F(n-2)
    
        
    //The recursive Fibonacci method 
    public int Recursivefibonacci(int n) {
       
        if(n==0) {
            return 0;
        } if(n==1) {
            return 1;
        }else {
            
        int fib = Recursivefibonacci(n-1)+Recursivefibonacci(n-2);
        return fib;
        }
            
    }
}

I cannot get this thing to print anything.我无法让这个东西打印任何东西。 How can I fix this?我怎样才能解决这个问题?

It's because you're not printing anything else.这是因为你没有打印任何其他东西。 Your method doesn't print anything (just returns a value), and your main doesn't print anything (aside from "enter number").你的方法不打印任何东西(只返回一个值),你的 main 不打印任何东西(除了“输入数字”)。

You can try changing: fibonacci_test.Recursivefibonacci(n);您可以尝试更改: fibonacci_test.Recursivefibonacci(n); to println (fibonacci_test.Recursivefibonacci(n));println (fibonacci_test.Recursivefibonacci(n));

It would be more appropriate to return Recursivefibonacci(n-1)+Recursivefibonacci(n-2);返回Recursivefibonacci(n-1)+Recursivefibonacci(n-2); rather than storing it in a variable.而不是将其存储在变量中。

I did this in python, you can have a look, try to adapt it to java, this might help you.我是在python做的,你可以看看,适配到java试试,或许对你有帮助。 Recursivity can be a headache but seems good.递归可能令人头疼,但看起来不错。

def fib(n):
    if n == 0:
        return 0
    if n <= 1:
        return 1
    res = (fib(n-1)+ fib(n-2))
    return res

/////// Probably your code will look somehow like this: /////// 您的代码可能看起来像这样:

public int Recursivefibonacci(int n) {
    if(n == 0){
       return 0;

}
if(n <= 1) {
   return 1;
    } 

    int fib = Recursivefibonacci(n-1)+Recursivefibonacci(n-2);
    return fib;
        
}

Try it.尝试一下。 and let me know if it worked.让我知道它是否有效。

Here is my main method, I am trying to call the Fibonacci sequence to tell me what number would be at the location the user inputs:这是我的主要方法,我试图调用斐波那契数列来告诉我用户输入位置的数字:

import java.util.Scanner; //import Scanner

public class Main {
    public static void main(String[] args) {
         
       System.out.println("enter number");
       Scanner input = new Scanner(System.in); 
       int n = input.nextInt();
         
       Fibonacci fibonacci_test = new Fibonacci();
       fibonacci_test.Recursivefibonacci(n);
         
    }
}

Here is my Fibonacci code that I have:这是我的斐波那契代码:

public class Fibonacci {
//Fn=F(n-1)+F(n-2)
    
        
    //The recursive Fibonacci method 
    public int Recursivefibonacci(int n) {
       
        if(n==0) {
            return 0;
        } if(n==1) {
            return 1;
        }else {
            
        int fib = Recursivefibonacci(n-1)+Recursivefibonacci(n-2);
        return fib;
        }
            
    }
}

I cannot get this thing to print anything.我不能让这个东西打印任何东西。 How can I fix this?我怎样才能解决这个问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM