简体   繁体   English

即使条件为true,Return语句也不会中断

[英]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被一次又一次地发现。

see http://ideone.com/1d440f 看到http://ideone.com/1d440f

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.

相关问题 即使条件仍然成立,while循环也不会继续 - While loop doesn't continue even though condition still holds true 为什么即使在 Selenium 中的 Java 中的条件为假时 else 块也不会被执行 - why an else block doesn't get executed even though the condition is false in Java in Selenium 在 j 处递增,即使它不满足条件 - Incrementing at j even though it doesn't meet the condition 编译器抱怨“缺少返回语句”,即使无法达到返回语句丢失的条件 - Compiler complains about “missing return statement” even though it is impossible to reach condition where return statement would be missing 得到了break语句,但是无法找出Java的条件 - Got the break statement to work, but can't figure out the java for condition 如果即使评估为真,语句也不会执行 - If statement wont execute even though evaluation is true 即使条件为真也不执行 if 语句 - not executing the if statement even when the condition is true 即使未在 return 语句中指定任何返回值,也会向调用者返回真值 - A true value is returned to the caller , even with out specifying any return value in the return statement 即使将“failsOnError”设置为“true”,Maven Checkstyle 插件在构建期间也不会失败 - Maven Checkstyle Plugin doesn't fail during build even though `failsOnError` is set to `true` 布尔值不会破坏语句 - Boolean doesn't break for statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM