简体   繁体   English

为什么返回执行两次

[英]Why is return executed twice

In this case the value does match and the value of boolean is set to true,however return is called twice and does update the value to false.Can anyone suggest what am I missing here?. 在这种情况下,值匹配并且boolean的值设置为true,但是返回被调用两次并且将值更新为false。任何人都可以建议我在这里缺少什么?

public class BinSearch {

    public static void main(String[] args) {

        BinSearch bin=new BinSearch();
        int arr[]= {2,4,6,8,10,12,14,16};
        boolean b=bin.binSearch(arr,0,arr.length-1,12);
        System.out.println("Number found "+b);

    }

    public  boolean binSearch(int arr[],int low,int high,int val)
    {
        int mid=(low+high)/2;

        if(arr[mid]==val)
        {
            return true;
        }

        else if(arr[mid]>val)
        {
            binSearch(arr,mid+1,high,val);
        }

        else  
        {
            binSearch(arr,low+1,mid,val);
        }

        return false;
    }
}

You're missing two returns when calling the recursion: 调用递归时,您丢失了两个返回值:

return binSearch(...);

If you don't write them, the method will ignore the result of the recursion and just return false at the end. 如果你不编写它们,该方法将忽略递归的结果,并在最后返回false After doing that, the last return will be unnecessary and should be deleted. 这样做之后,最后一次return将是不必要的,应该删除。 Finally, you need to check the case when low > high , that means that the element was not found. 最后,您需要在low > high时检查案例,这意味着找不到该元素。

Because your return false; 因为你的return false; overrides everything except for a case when the value is found on the first run and no recursive call is invoked. 除了在第一次运行时找到值并且不调用递归调用的情况时,覆盖所有内容。 Please return for each recursive call. 请返回每个递归电话。 Additionally, you must check if your low boundary is less or equal to the high boundary. 此外,您必须检查您的低边界是否小于或等于高边界。 So, your code could be as follows: 所以,你的代码可能如下:

public boolean binSearch(int arr[],int low,int high,int val)
{
    if (low <= high) {
       int mid=(low+high)/2;
       if(arr[mid]==val) {
          return true;
       } else if(arr[mid]>val) {
          return binSearch(arr,mid+1,high,val);
       } else {
          return binSearch(arr,low+1,mid,val);
       }
    }
    return false;
}

You can also try the following code in binSearch function. 您还可以在binSearch函数中尝试以下代码。 The only change made is that there is a boolean type variable capturing the value returned in the recursive calls and finally returning this varaible. 唯一的变化是有一个布尔类型变量捕获递归调用中返回的值,最后返回此变量。

public  boolean binSearch(int arr[],int low,int high,int val) {
    int mid=(low+high)/2;
    boolean returnValue = false;

    if(arr[mid]==val) {
        return true;
    } else if(arr[mid]>val) {
        returnValue  = binSearch(arr,mid+1,high,val);
    } else {
        returnValue  = binSearch(arr,low+1,mid,val);
    }    
    return returnValue;
}
public class BinSearch {

    public static void main(String[] args) {

        BinSearch bin=new BinSearch();
        int arr[]= {2,4,6,8,10,12,14,16};
        boolean b=bin.binSearch(arr,0,arr.length-1,16);
        System.out.println("Number found "+b);

    }

    public  boolean binSearch(int arr[],int low,int high,int val)
    {
        int mid=(low+high)/2;
        //boolean b = false;
        while(low<=high)
        {
            if(arr[mid]==val)
            {
                return true;
            }

            else if(val>arr[mid])
            {
                return binSearch(arr,mid+1,high,val);
            }

            else  
            {
                return binSearch(arr,low+1,mid,val);
            }

        }

        return false;
    }
}

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

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