[英]Return statement doesn't break out of block, even though condition is true
This is a simple program to check if a number if Fibonnacci. 这是一个简单的程序,用于检查斐波那契是否为数字。 I have a mysterious bug: the "return true" statement isn't triggered.
我有一个神秘的错误:“ return true”语句未触发。 Instead, "hi" will be printed many times.
相反,“ hi”将被打印多次。 Return should break out of the method, does anyone have insight as to why it's not?
回报应该突破这种方法,有谁知道为什么不这样做? Thanks!
谢谢!
import java.util.*;
public class Solution {
public static boolean listFibs (long oldestFib, long oldFib, long input) {
long newFib = oldestFib + oldFib;
while (newFib < Math.pow(10,10)) {
if (newFib == input) {
System.out.println("hi");
return true;
}
listFibs(oldFib, newFib, input);
}
return false;
}
public static void main(String[] args) {
/*Scanner in = new Scanner(System.in);
int testCases = in.nextInt();
for (int i = 0; i < testCases; i++) {
int a = in.nextInt();
System.out.println("A= " + a);
System.out.println(listFibs(0, 1, a));
}*/
System.out.println(listFibs(0, 1, 5));
} } }}
Due to the recursion there are many incarnations of listFibs
. 由于递归,
listFibs
有很多形式。 The return
just leaves one of them. return
只剩下其中之一。
In the example given, you get the following calls: 在给出的示例中,您得到以下调用:
listFib(0,1,5)
listFib(1,1,5)
listFib(1,2,5)
listFib(2,3,5)
-> true
listFib(2,3,5) // called again due to the loop
-> true
listFib(2,3,5) // called again due to the loop
-> true
listFib(2,3,5) // called again due to the loop
-> true
listFib(2,3,5) // called again due to the loop
...
actually, if "hi" is printed many times, the return statement must be executed the same time. 实际上,如果多次打印“ hi”,则return语句必须同时执行。 if you install a break point in that return statement, you will see.
如果在该return语句中安装断点,则会看到。
your function is a recursion
, and "listFibs" will be called many times. 您的函数是
recursion
,“ listFibs”将被多次调用。
Print values of all 3 variables in the method and you will know what's wrong. 打印方法中所有3个变量的值,您将知道出了什么问题。 You are using recursion, see how many times it's being called.
您正在使用递归,请查看它被调用了多少次。
Also, after call to listFib
, execution will go to while loop again. 同样,在调用
listFib
,执行将再次转到while循环。 You need to say return listFibs
at least. 您至少需要说
return listFibs
。 Between your listFibs
and while loop condition, nothing is changing. 在
listFibs
和while循环条件之间,没有任何变化。 2,3,5 are being found again and again. 2,3,5被一次又一次地发现。
You are one step short... The recursive call doesn't do anything with the results you get from listFibs, so the program sees something like this: 您只差了一步...递归调用对从listFibs获得的结果不执行任何操作,因此程序将看到如下内容:
while (newFib < Math.pow(10,10)) {
if (newFib == input) {
System.out.println("hi");
return true;
}
true //or false
}
Try adding this extra little IF statement. 尝试添加此多余的IF语句。 Once a true result is found it will be passed back up the chain and out of the function.
一旦找到正确的结果,它将被返回链上并从函数中传递出去。
while (newFib < Math.pow(10,10)) {
if (newFib == input) {
System.out.println("hi");
return true;
}
if listFibs(oldFib, newFib, input){
return true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.