简体   繁体   中英

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. Instead, "hi" will be printed many times. 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 . The return just leaves one of them.

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. if you install a break point in that return statement, you will see.
your function is a recursion , and "listFibs" will be called many times.

Print values of all 3 variables in the method and you will know what's wrong. You are using recursion, see how many times it's being called.

Also, after call to listFib , execution will go to while loop again. You need to say return listFibs at least. Between your listFibs and while loop condition, nothing is changing. 2,3,5 are being found again and again.

see 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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