gpt4 book ai didi

java - 线性搜索在应该返回下标数字时返回未找到

转载 作者:行者123 更新时间:2023-12-02 08:03:18 25 4
gpt4 key购买 nike

我正在做一个 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;
}
}

最佳答案

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

  1. 如果您想搜索 Int,那么我也会将 arrayBuild 设为 int 类型。或者,您可以通过将objective 的类型更改为 double 并让用户输入 double 来搜索 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. 您没有在主程序中的任何位置调用 arraySort

关于java - 线性搜索在应该返回下标数字时返回未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8568992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com