【问题标题】:java array searching methodjava数组查找方法
【发布时间】:2015-04-03 17:01:13
【问题描述】:

我必须创建一个数组,其 10 个数字由用户通过扫描仪选择。程序按升序排列 10 个数字,然后打印出新列表。然后它要求用户输入任何数字,然后使用二进制搜索来查看该数字是否在 10 个数字的列表中。

这是我目前所拥有的:

import java.util.Scanner;

public class lab11 {

    public static void main(String[] args){
        double[] numbers = new double[10];        
        System.out.println("Please enter 10 double values:");
        for (int i=0;i<10;i++){
            numbers[i] = inputArray();
        }
        System.out.println("sorting");
        print(selectionSort(numbers));

        System.out.println("Please enter a search key:");

    }
    public static double inputArray(){

        Scanner input = new Scanner(System.in);
        System.out.print(">");
        double d = input.nextInt();
        return d;
    }

    public static double[] selectionSort(double[] list){
        double temp;
        for(int i=0; i < (list.length-1); i++){
            for(int j = i+1; j < list.length; j++){
                if(list[j] < list[i]){
                    temp = list[i];
                    list[i] = list[j];
                    list[j] = temp;
                }
            }
        }
        return list;
    }
    public static void print(double[] arr){
        for(double d:arr){

            System.out.println("list["+j+"]"+" = "+d);

        }

    }
    public static int binarySearch(double[]list, double key){

        int low=0,high=list.length -1;

        key =input.nextInt();

        while(high>=low){
            int mid=(low+high)/2;
            if(key<list[mid])
                    high=mid-1;
            else if(key==list[mid]) return mid;
            else low=mid+1;
        }
        return-1;
        }

    }

我在两件事上需要帮助。

(1) 在 Print 方法下。我希望程序像“list [i] = d”一样打印出用户输入的数字d,它工作正常,但i没有。我希望它是 0 到 9 的数组编号。

(2) 我需要有关调用二分搜索的帮助,以便我可以得到搜索的输出。

【问题讨论】:

  • 你为什么用double d = input.nextInt();而不是double d = input.nextDouble();
  • 我不是故意的,谢谢
  • 你能帮我用二分查找法吗?如何调用该方法?
  • 我建议将 double key = input.nextDouble() 移动到 main 方法中(当然,您需要事先 final Scanner input = new Scanner(System.in);)并通过 int position = binarySearch(numbers, key) 在 Mark Lee 的答案中调用您的方法或 binarySearch 方法。如何解释position 中的值你应该很清楚。
  • 我做了所有的更改,我允许我输入一个搜索键,但我没有输出,因为我没有。像这个搜索键不在数组中。我会将 System.out.print(......) 放在哪里。我是放在main方法还是二分查找?

标签: java arrays overloading


【解决方案1】:

为了正确的输入,你可以写一个do...while循环,循环直到输入有效:

for (int i = 0; i < 10; i++) {
        double number;
        Scanner input = new Scanner(System.in);
        do {
            System.out.print(">");
            number = input.nextDouble();
        } while (number < 0 || number > 10);
    }
}

【讨论】:

    【解决方案2】:

    对于您似乎想要使用的输出

    // [...] perform the sorting [...]
    
    // enter the search key
    System.out.println("Please enter a search key:");
    final int position = Arrays.binarySearch(numbers, input.nextDouble());
    if (position < 0) {
        System.out.println("key is not in the list");
    } else {
        System.out.println("key is at position " + position);
    }
    

    【讨论】:

    • 我会把它放在哪里,你能把整个事情写出来吗?很抱歉,我了解过载的整个概念和每种方法,但是在将它们放在一起时完全混淆了。
    【解决方案3】:

    我会这样写

    public static void main(String[] args) {
    
        // initialize
        double[] numbers = new double[10];
        Scanner input = new Scanner(System.in);
    
        // insert variables into array
        inputArray(numbers, input);
    
        // start sorting
        System.out.println("sorting");
        selectionSort(numbers);
    
        // print the sorted array
        print(numbers);
    
        // binary search
        binarySearch(numbers, input);
    
        // close scanner
        input.close();
    }
    
    public static void inputArray(double[] numbers, Scanner input) {
        System.out.println("Please enter 10 double values:");
        for (int i = 0; i < 10; i++) {
            System.out.print(">");
            numbers[i] = input.nextDouble();
        }
    }
    
    public static void print(double[] numbers) {
        for (int i = 0; i < numbers.length; i++) {
            System.out.println("list[" + i + "]" + " = " + numbers[i]);
        }
    }
    
    public static double[] selectionSort(double[] list) {
        double temp;
        for (int i = 0; i < (list.length - 1); i++) {
            for (int j = i + 1; j < list.length; j++) {
                if (list[j] < list[i]) {
                    temp = list[i];
                    list[i] = list[j];
                    list[j] = temp;
                }
            }
        }
        return list;
    }
    
    public static void binarySearch(double[] numbers, Scanner input) {
        System.out.println("Please enter a search key:");
        double key = input.nextDouble();
        int index = Arrays.binarySearch(numbers, key);
        if (index < 0) {
            System.out.println(key + " is not in the list");
        } else {
            System.out.println(key + " is in the list, index = " + index);
        }
    }
    

    【讨论】:

    • 我复制了代码并试了一下。它仍然关闭。所以说我输入了数字:10,9,8,7,6,5,4,3,2,1。然后将它们排序为:1,2,3,4,5,6,7,8,9,10。那工作很好。然后它要求输入搜索键,我输入 45。应该说 45 不在列表中。相反,它写了-11。我怎么能这么说?
    • 我必须保留 inputArray 方法,它是分配的一部分。我非常感谢你的帮助。你能把它安排到我仍然可以保留方法 inputArray 的地方吗?我必须保留其中的所有方法。
    • 已编辑。我认为在这里发布你的作业不是一个好主意。
    • 老兄,非常感谢。谢谢大家。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 2022-12-31
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    相关资源
    最近更新 更多