有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

初学者二进制搜索中的java错误

当数组中存在搜索值(SV)时,它显示输出。但如果它不在那里就不会。它不断请求输入并不断循环

这是我第一次尝试为二进制搜索编写代码

import java.util.Scanner;

public class Binary_search {

    public static void main() {
        Scanner input = new Scanner(System.in);
        int start = 0;
        int end = arr.length;

        //I used flag to end the loop 
        int Flag = 0;
        int mid = 0;
        int SV = input.nextInt();

        //here I enter values in the array
        for (int x = 0; x <= 4; x++) {
            arr[x] = input.nextInt();
        }

        //here I start a loop for the search 
        while (Flag == 0) {
            mid = (start + end) / 2;

            if (mid == SV) {
                System.out.println("Number Found" + arr[mid]);
                Flag = Flag + 1;
            } else if (mid > SV) {
                end = mid - 1;
            } else if (mid < SV) {
                start = mid + 1;
            }

            //this was the second possibility if the number was not present   
            else if (start == end) {
                Flag = Flag + 1;
                System.out.println("Number not found");
            }
        }
    }
}

如果SV存在于阵列中,它将显示其所处的位置,“找到的编号”+arr[mid]。但如果它不在那里,它应该输出“找不到数字”,然而,这不会发生,它会不断请求输入


共 (2) 个答案

  1. # 1 楼答案

    您可以创建这样一个类来输入用户输入的数字,并将其存储在数组中,然后使用二进制搜索在数组中搜索另一个输入的数字,并显示其位置

    import java.util.*;
    class BinarySearch
    {
       public static void main()
       {
          int i, num, item, array[], first, last, middle;
          Scanner sc = new Scanner(System.in);
          System.out.println("Enter number of elements:");
          num = sc.nextInt();
          //array gets created by inputted space
          array = new int[num];
          System.out.println("Enter " + num + " integers");
          for (i = 0; i < num; i++)
              array[i] = sc.nextInt();
    
          System.out.println("Enter the number to be searched:");
          item = sc.nextInt();
          first = 0;
          last = num - 1;
          middle = (first + last)/2;
    
          //search starts
          while( first <= last )
          {
             if ( array[middle] < item )
               first = middle + 1;
             else if ( array[middle] == item )
             {
               //number found in
               System.out.println(item + " is found at location " + (middle + 1));
               break;
             }
             else
             {
                 last = middle - 1;
             }
             middle = (first + last)/2;
          }
          if ( first > last )
              //number not found in array
              System.out.println(item + " is not found.\n");
       }
    }
    
  2. # 2 楼答案

    您可以这样简单地创建。 请比较并检查需要更正的地方

    public class Test {
    
        public static void main(String[] args) {
            int arr[] = { 10, 20, 30, 40, 50 };
            int key = 3;
            binarySearchExample(arr, key);
        }
    
        public static void binarySearchExample(int arr[], int key) {
            int first = 0;
            int last = arr.length - 1;
            int mid = (first + last) / 2;
            while (first <= last) {
                if (arr[mid] < key) {
                    first = mid + 1;
                } else if (arr[mid] == key) {
                    System.out.println("Element is found at index: " + mid);
                    break;
                } else {
                    last = mid - 1;
                }
                mid = (first + last) / 2;
            }
            if (first > last) {
                System.out.println("Element is not found!");
            }
        }
    
    }