斐波那契数列不会打印任何内容,只会输出用户输入的数字。

huangapple go评论68阅读模式
英文:

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

问题

以下是翻译好的部分:

这是我的主要方法,我正在尝试调用斐波那契数列,以告诉我用户输入位置处的数字是多少:

import java.util.Scanner; //导入Scanner类

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

这是我拥有的斐波那契代码:

public class Fibonacci {
//Fn=F(n-1)+F(n-2)
    
    //递归斐波那契方法 
    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;
        }
            
    }
}

我无法使这个程序打印出任何东西。我该如何修复这个问题?

英文:

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?

答案1

得分: 5

因为你没有打印任何其他内容。
你的方法没有打印任何内容(只是返回一个值),而且你的主函数也没有打印任何内容(除了“输入数字”)。

你可以尝试将:fibonacci_test.Recursivefibonacci(n); 更改为 println(fibonacci_test.Recursivefibonacci(n));

英文:

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").

You can try changing: fibonacci_test.Recursivefibonacci(n); to println (fibonacci_test.Recursivefibonacci(n));

答案2

得分: 1

更合适的做法是返回 Recursivefibonacci(n-1)+Recursivefibonacci(n-2); 而不是将其存储在变量中。

英文:

It would be more appropriate to return Recursivefibonacci(n-1)+Recursivefibonacci(n-2);rather than storing it in a variable.

答案3

得分: 1

以下是您要翻译的内容:

我在Python中做了这个,你可以看一下,尝试将它适应到Java,这可能会帮助你。递归可能会让人头疼,但看起来很不错。

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

可能您的代码会看起来像这样:

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

试试吧!并且让我知道是否有效。

英文:

I did this in python, you can have a look, try to adapt it to java, this might help you. Recursivity can be a headache but seems good.

def fib(n):
    if n == 0:
        return 0
    if n &lt;= 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 &lt;= 1) {
   return 1;
    } 

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

Try it! and let me know if it worked.

huangapple
  • 本文由 发表于 2020年8月30日 13:35:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63654298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定