当应该返回下标数时,未找到线性搜索返回

时间:2011-12-19 23:33:24

标签: java arrays sorting search

我正在为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;
    }
}

2 个答案:

答案 0 :(得分:1)

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

    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成为双精度而非整数。

答案 1 :(得分:1)

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

  1. 如果你想搜索Ints,那么我也会使arrayBuild类型为int。或者您可以通过将objective的类型更改为double并让用户输入双重

    来搜索双打
  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. 您没有在主

  5. 中的任何地方致电arraySort