整数变量将0存储为方法的返回值。

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

Integer variable stores 0 as method return value

问题

在Java中,我试图使用递归程序执行二分搜索,算法似乎完全正确,但我从递归函数中存储结果的变量却将值存储为0。
在以下代码中,我想将找到的元素的索引存储在变量result中,但输出将result的值打印为0。
当我在返回语句之前打印mid的值时,该值是正确的。如何解决这个问题?

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n;
        System.out.println("Enter the number of elements in the array: ");
        n = scanner.nextInt();

        int arr[] = new int[n];
        System.out.println("Enter the array elements (from index 0): ");
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }

        int ele;
        System.out.println("Enter the element to be searched: ");
        ele = scanner.nextInt();
/*************************************************************************************/
        int result = binarySearchRecursive(arr, 0, n - 1, ele);
        System.out.println(result);
/************************************************************************************/

        if (result == -1) {
            System.out.println(ele + " not found");
        } else {
            System.out.println(ele + " found at index: " + result);
        }


    }

    //Algorithm

    public static int binarySearchRecursive(int arr[], int l, int r, int ele) {

        //检查是否存在单个元素
        if (l == r) {
            if (arr[l] == ele) {
                return l;
            } else {
                return -1;
            }
        } else {      //多个元素
            int mid = (l + r) / 2;

            //检查条件
            if (ele == arr[mid]) {
                System.out.println("Method return 'mid' value: " + mid);
                return mid;
            } else if (ele < arr[mid]) {
                return binarySearchRecursive(arr, l, mid - 1, ele);
            } else {
                return binarySearchRecursive(arr, mid + 1, r, ele);
            }

        }

    }
}

输出在这里:输出图片

英文:

I was trying Binary Search recursive program in Java, the algorithm seems to be perfectly fine, but the variable in which I store the result from the recursive function, is storing 0 as the value.
In the following code, I want to store the index of the element found in the variable result, but the output prints result's value as 0.
When I print the value of mid before the return statement, the value is correct. How to fix this problem??

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
System.out.println(&quot;Enter the number of elements in the array: &quot;);
n = scanner.nextInt();
int arr[] = new int[n];
System.out.println(&quot;Enter the array elements (from index 0): &quot;);
for (int i = 0; i &lt; n; i++) {
arr[i] = scanner.nextInt();
}
int ele;
System.out.println(&quot;Enter the element to be searched: &quot;);
ele = scanner.nextInt();
/*************************************************************************************/
int result = binarySearchRecursive(arr, 0, n - 1, ele);
System.out.println(result);
/************************************************************************************/
if (result == -1) {
System.out.println(ele + &quot; not found&quot;);
} else {
System.out.println(ele + &quot; found at index: &quot; + result);
}
}
//Algorithm
public static int binarySearchRecursive(int arr[], int l, int r, int ele) {
//Check whether a single element is present
if (l == r) {
if (arr[l] == ele) {
return l;
} else {
return -1;
}
} else {      //Multiple elements
int mid = (l + r) / 2;
//Check conditions
if (ele == arr[mid]) {
System.out.println(&quot;Method return &#39;mid&#39; value: &quot;+mid);
return mid;
} else if (ele &lt; arr[mid]) {
binarySearchRecursive(arr, l, mid - 1, ele);
} else {
binarySearchRecursive(arr, mid + 1, r, ele);
}
}
return -l;
}
}

Here is the output

答案1

得分: 6

你应该返回递归调用的值:

        if (ele == arr[mid]) {
System.out.println("方法返回 'mid' 值:" + mid);
return mid;
} else if (ele < arr[mid]) {
return binarySearchRecursive(arr, l, mid - 1, ele);
} else {
return binarySearchRecursive(arr, mid + 1, r, ele);
}
英文:

You should return the value of the recursive calls:

        if (ele == arr[mid]) {
System.out.println(&quot;Method return &#39;mid&#39; value: &quot;+mid);
return mid;
} else if (ele &lt; arr[mid]) {
return binarySearchRecursive(arr, l, mid - 1, ele);
} else {
return binarySearchRecursive(arr, mid + 1, r, ele);
}

答案2

得分: 0

你需要在binarySearchRecursive的递归函数调用中添加return语句。目前你的代码总是会落到最后的return语句,它只会返回 -l => 这会导致在binarySearchRecursive(arr, 0, n - 1, ele)中返回 0。

    return binarySearchRecursive(arr, l, mid - 1, ele);
return binarySearchRecursive(arr, mid + 1, r, ele);
英文:

You have to add return statements to the recursive function calls of binarySearchRecursive. Right now your code is always falling through to the last return statement which just returns -l => which returns 0 for binarySearchRecursive(arr, 0, n - 1, ele);

    return binarySearchRecursive(arr, l, mid - 1, ele);
return binarySearchRecursive(arr, mid + 1, r, ele);

huangapple
  • 本文由 发表于 2020年9月29日 14:58:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64114413.html
匿名

发表评论

匿名网友

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

确定