【问题标题】:binary search from txt excercise文本练习中的二进制搜索
【发布时间】:2023-03-14 21:24:01
【问题描述】:

编写一个程序,从文本文件中读取整数,然后对数字进行排序。然后使用笔记中的二分搜索算法(第 6 周)要求用户输入一个数字,如果该数字不在列表中,则显示该数字在哪个位置或未找到。

到目前为止我的代码:

import java.util.Arrays;

public class myBinarySearch
{
    public static void main(String[] args)
    {
        int i;
        int target, found;
        int[] numArray = {36, 27, 29, 15, 16, 39, 11, 31};

        Arrays.sort(numArray);
        for(i=0; i <numArray.length; i++)
        System.out.print(numArray[i] + " ");

        System.out.print("\nEnter the number you are searching for: ");
        target = ReadKb.getInt();
        found = theBinarySearch(numArray, target);
        if(found == -1)
        System.out.println("Number not found");
        else
        System.out.println("Number: " +target +" found at position: " +found);
    }

    //Method searching using binary search algorithm
    private static int theBinarySearch(int []numArray, int target)
    {
        int mid,bottom,top;
        mid=0;
        bottom=0;
        top=numArray.length-1;
        while (top>= bottom)
        {
            mid=(top +bottom)/2;
            if(target==numArray[mid])
                break;
            else
                if(target<numArray[mid])
                    top=mid-1;
                else
                    bottom=mid+1;
        }

        if(target==numArray[mid])
            return mid;
        else
            return -1;
    }

}

还有txt文档:

数字.txt 55 12 88 33 25 5 3 23 64 21

【问题讨论】:

    标签: java arrays search binary


    【解决方案1】:

    您已经为binarySerach 编写了代码,唯一缺少的就是从文件中读取它。

    如何从文件中读取数字?

    试试这个

        ....
        List<Integer> list = new ArrayList<Integer>();
    
        Scanner scanner = new Scanner(new File("resources/abc.txt"));
        while (scanner.hasNextInt()) {
            list.add(scanner.nextInt());
        }
    
        Integer[] numArray = list.toArray(new Integer[] {});
    
        Arrays.sort(numArray);
        ....
    

    完整代码如下:

    import java.io.File;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;
    
    public class myBinarySearch {
        public static void main(String[] args) throws Exception {
            int i;
            int target, found;
            // int[] numArray = { 36, 27, 29, 15, 16, 39, 11, 31 };
    
            List<Integer> list = new ArrayList<Integer>();
    
            Scanner scanner = new Scanner(new File("resources/abc.txt"));
            while (scanner.hasNextInt()) {
                list.add(scanner.nextInt());
            }
    
            Integer[] numArray = list.toArray(new Integer[] {});
    
            Arrays.sort(numArray);
            for (i = 0; i < numArray.length; i++)
                System.out.print(numArray[i] + " ");
    
            System.out.print("\nEnter the number you are searching for: ");
            target = new Scanner(System.in).nextInt();
            found = theBinarySearch(numArray, target);
            if (found == -1)
                System.out.println("Number not found");
            else
                System.out.println("Number: " + target + " found at position: " + found);
        }
    
        // Method searching using binary search algorithm
        private static int theBinarySearch(Integer[] numArray, int target) {
            int mid, bottom, top;
            mid = 0;
            bottom = 0;
            top = numArray.length - 1;
            while (top >= bottom) {
                mid = (top + bottom) / 2;
                if (target == numArray[mid])
                    break;
                else if (target < numArray[mid])
                    top = mid - 1;
                else
                    bottom = mid + 1;
            }
    
            if (target == numArray[mid])
                return mid;
            else
                return -1;
        }
    
    }
    

    【讨论】:

    • 你再一次明确地帮了我大忙! Braj,你是最棒的,继续努力,谢谢你帮助我。奇怪的是,去年的 cert 4 是 Visual Basics 现在是今年的高级文凭直接进入 Java 感谢您的所有帮助,非常感谢
    【解决方案2】:

    您尚未定义 ReadKB。在我的系统上显示:

    mohit@mohit:~/somewhere$ javac myBinarySearch.java 
    myBinarySearch.java:16: error: cannot find symbol
            target = ReadKb.getInt();
                     ^
      symbol:   variable ReadKb
      location: class myBinarySearch
    

    选择某项作为

    Scanner in = new Scanner();
    and target = in.nextInt();
    

    【讨论】:

    • OP 想以这种方式做到这一点new Scanner(System.in).nextInt()。读取用户的输入。
    猜你喜欢
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 2017-06-13
    • 2013-07-14
    相关资源
    最近更新 更多