【发布时间】:2021-02-22 14:40:12
【问题描述】:
正在处理一项任务,但我一直遇到错误。我不太擅长Java,所以我不确定我做错了什么。作业要我评估递归二进制搜索,我在 binarySearch 方法中发现错误;它说“列表= {”1,2,5,7,9,15“};”是一个非法的表达式开头,而不是一个语句,并且 ';'预期的,即使显然有一个。我不知道...我有点压力了。
方法如下:
//recursive binary search
public boolean binarySearch(int[] list, int target, int low, int high) {
list ={"1,2,5,7,9,15"};
target = 5;
boolean result = binarySearch(list, target, 0, list.length-1);
executions++;
int mid = (low + high) / 2;
if(!result){
System.out.println("Target not found -- bad search.");
}else{
System.out.println("Target found -- sucessful search!");
}
if (list[mid] == target) {
return true;
} else {
if (low > high) {
return false;
} else {
comparisons++;
if (list[mid] < target) {
return binarySearch(list, target, mid + 1, high);
} else {
return binarySearch(list, target, low, mid - 1);
}
}
这是完整的代码:
public class Problem2 {
public static int executions = 0;
public static int comparisons = 0;
public static int[] numbers = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
public static void main(String[] args) {
System.out.println("2nd fibonacci number: " + fibonacci(2));
System.out.println("Number of executions: " + executions);
System.out.println("Number of comparisons: " + comparisons);
executions = 0;
comparisons = 0;
System.out.println();
System.out.println("Number of operations: " + executions);
System.out.println("Number of comparisons: " + comparisons);
executions = 0;
comparisons = 0;
System.out.println();
System.out.println("Factorial of 7: " + nFactorial(7));
System.out.println("Number of operations: " + executions);
System.out.println("Number of comparisons: " + comparisons);
executions = 0;
comparisons = 0;
}
//recursive Nfactorial
public static int nFactorial(int n) {
comparisons++;
if (n <= 1) {
return 1;
} else {
executions++;
return n * nFactorial(n - 1);
}
}
//recursive fibonacci number
public static int fibonacci(int n) {
comparisons++;
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
executions++;
return fibonacci(n - 1) + fibonacci (n - 2);
}
}
//recursive binary search
public boolean binarySearch(int[] list, int target, int low, int high) {
list ={"1,2,5,7,9,15"};
target = 5;
boolean result = binarySearch(list, target, 0, list.length-1);
executions++;
int mid = (low + high) / 2;
if(!result){
System.out.println("Target not found -- bad search.");
}else{
System.out.println("Target found -- sucessful search!");
}
if (list[mid] == target) {
return true;
} else {
if (low > high) {
return false;
} else {
comparisons++;
if (list[mid] < target) {
return binarySearch(list, target, mid + 1, high);
} else {
return binarySearch(list, target, low, mid - 1);
}
}
}
}
}
如果可以,请提供帮助。如果您发现任何其他错误,请告诉我。
【问题讨论】:
-
不幸的是,您的问题表明您在这里遗漏了一些非常基本的概念,这些概念在本论坛的问答形式中很难解释。我建议您阅读标准 Java 教程(例如 docs.oracle.com/javase/tutorial),如果您有具体问题,请返回此处。
-
@zaibeh,正如 AKSSingh 所指出的,您的初始化不正确。另一方面,您不应该在二分搜索函数中初始化。您应该重用传递给函数的输入数组和目标值
-
正确方法:
list = new int[]{1, 2, 5, 7, 9, 15};但是由于代码中的逻辑缺陷,这样做会导致进一步的错误。检查@Horse 答案以了解正确的逻辑。 请访问此链接以获取有关如何在 Java 中声明和初始化数组的更多信息。请阅读第一个答案中的前两个 cmets,因为它们包含与您的问题类似的信息。 stackoverflow.com/questions/1200621/…
标签: java recursion binary binary-search