【问题标题】:Linear search returning not found when it should be returning subscript number当它应该返回下标数字时,找不到返回的线性搜索
【发布时间】:2012-01-24 00:51:07
【问题描述】:

我正在处理 Java 的家庭作业,其中一个程序应该从用户那里读取 10 个数字,然后要求一个数字进行搜索。它对数字进行排序(升序)并对数组执行线性搜索,然后应该返回找到它的下标或显示未找到的消息。

即使应该返回下标,我的代码也会给我“未找到”响应。你能看看我的代码吗?我试过改变我的 if 语句,但没有帮助。我已经尽可能地使用了书中关于搜索和排序方法的示例(请随时指出错误)。

这里是代码。

package program11;
import java.util.Scanner;

public class ArraySearch {

    public static void main(String[] args) {
        double[] arrayBuild = new double[10];
        Scanner input = new Scanner(System.in);

        for (int i = 0; i < arrayBuild.length; i++) {
            System.out.print("Enter a number.");
            arrayBuild[i] = input.nextDouble();
        }

        System.out.print("Enter a number to search for ");
        int objective = input.nextInt();

        linearCheck(arrayBuild, objective);

        if ((objective >= 0) && (objective < arrayBuild.length)) {
            System.out.println("Found at index: " + objective);
        } else {
            System.out.println("Not Found");
        }
    }

    public static void arraySort(double[] arrayBuild) {
        for (int i = 1; i < arrayBuild.length; i++) {
            double currentPoint = arrayBuild[i];
            int r;
            for (r = i - 1; r >= 0 && arrayBuild[r] > currentPoint; r--) {
                arrayBuild[r + 1] = arrayBuild[r];
            }
            arrayBuild[r + 1] = currentPoint;
        }
    }

    public static double linearCheck(double[] arrayBuild, int objective) {
        for (int i = 0; i < arrayBuild.length; i++) {
            if (objective == arrayBuild[i])
                return i;
        }
        return -1;
    }

}

编辑-新代码。现在一切都完成了,除了当我输入一个不在数组中的数字时,我得到一个否定的结果。例如,输入 10 8 7 6 5 3 5 3 5 3 5 6 并搜索 11,线性搜索的结果为 -1,二分搜索的结果为 -11。我尽我所能接受了你的建议。我现在缺少什么?

package program11;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class ArraySearch {

    public static void main(String[] args) {
        double[] arrayBuild = new double[10];
        Scanner input = new Scanner(System.in);
        int reply = 2;

for (int i = 0; i < arrayBuild.length; i++) {
    System.out.print("Enter a number.");
    arrayBuild[i] = input.nextDouble();
        }
while (reply != 1) {
        System.out.print("Enter a number to search for ");
        double objective = input.nextDouble();

arraySort(arrayBuild);
        double linearResult = linearCheck(arrayBuild, objective);

if (objective >= 0) {
        System.out.println("Linear search found at index: " + linearResult);
                }
else {
        System.out.println("Not Found (linear)");
        }
        double binaryResult = binaryCheck(arrayBuild, objective);

if (objective >= 0) {
System.out.println("Binary search found at index: " + binaryResult);
                }
else {
     System.out.println("Not Found (binary)");
        }
reply = JOptionPane.showConfirmDialog(null, "Continue?"); 
    }
}

public static void arraySort(double[] arrayBuild) {
    for (int i = 1; i < arrayBuild.length; i++) {
        double currentPoint = arrayBuild[i];
        int r;
        for (r = i - 1; r >= 0 && arrayBuild[r] > currentPoint; r--) {
            arrayBuild[r + 1] = arrayBuild[r];
        }
        arrayBuild[r + 1] = currentPoint;
    }
    }

public static double linearCheck(double[] arrayBuild, double objective) {
    for (int i = 0; i < arrayBuild.length; i++) {
        if (objective == arrayBuild[i])
            return i;
    }
return -1;
    }
public static double binaryCheck(double[] arrayBuild, double objective) {
    int low = 0;
    int high = arrayBuild.length - 1;

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

【问题讨论】:

  • 这不是我看到的行为。你在哪里对数组进行排序?
  • 我在arraySort方法下对数组进行排序。它对你有用吗?如果我输入 10 个数字,其中包括 1,并且我选择 1 作为我的搜索目标,我会得到未找到的响应。 =/
  • @Chris 我没说它有效;它没有——但这是一个简单的输出错误。另外,为什么objectiveint,而数组条目是double
  • @user1082706 Buuuuut...你在哪里打电话 arraySort?它不会调用自己。另外,“找到”/“未找到”输出什么?
  • 我没有看到 the 问题,但是:(1) 没有任何东西永远不会调用arraySort,所以你实际上并没有对数组进行排序; (2) 您通常不应该在doubles 上进行== 比较,因为它们不存储精确值; (3) 奇怪的是,您的 linearCheck 正在 doubles 的数组中搜索 int。但我认为这些都不是 问题。 :-/

标签: java arrays sorting search


【解决方案1】:

我不确定是什么导致了您的问题,但我发现您的代码存在一些问题

  1. 如果您想搜索 Int,那么我也会将 arrayBuild 设为 int 类型。或者您可以通过将objective 的类型更改为双精度并让用户输入双精度来搜索双精度

  2. 当您调用linearCheck 时,您不会将结果存储在任何地方,您应该将其分配给变量

    double foundLocation = linearCheck(arrayBuild, objective);
    
  3. 您的 if 语句没有多大意义,您应该使用 linearCheck 的返回值,如下所示

    if (foundLocation >= 0) {
        System.out.println("Found at index: " + foundLocation);
    } else {
        System.out.println("Not Found");
    }
    
  4. 你没有在你的 main 中的任何地方调用 arraySort

【讨论】:

  • 谢谢!这有帮助,我想通了。现在唯一的问题是它正在返回数字,我应该得到下标。我会再搞砸一些。
【解决方案2】:

您永远不会保存搜索的价值。你的目标和你的结果不一样。这是我要做的代码更改:

    int result = linearCheck(arrayBuild, objective);

    if ((result >= 0) && (result < arrayBuild.length)) {
        System.out.println("Found at index: " + result);
    } else {
        System.out.println("Not Found");
    }

此更改要求您为线性检查例程返回一个 int。

这是为了使 objective 成为双精度而不是整数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-09
    • 2016-01-14
    • 2017-07-29
    • 1970-01-01
    • 2012-07-07
    • 2019-03-02
    • 2022-01-10
    • 2016-12-05
    相关资源
    最近更新 更多