方法在Java中没有返回任何东西
我用5种方法在Java中编写了一个类。线性搜索,如果找到该值,则返回true;如果未找到,则返回false。线性搜索2,返回值的位置,如果找到。二进制搜索。它也搜索数组中的值,打印Int数组(每次打印10个数字)以及选择排序,它对数组进行排序,以便我可以执行二分搜索。一切都很好,但由于某种原因,我的方法都没有返回任何东西(void printIntArray方法除外)。方法在Java中没有返回任何东西
编辑:
谢谢,伙计们,我没有意识到我需要的。出于某种原因,我认为它会自行返回价值......但另一个问题。 binarySearch方法似乎没有做任何事情。在打印语句“使用二分搜索在随机数组中搜索11”....之后,不会打印任何内容。
编辑2: 我的binarySearch方法是行不通的,因为我不小心过中旬+ 1两种else语句(否则,如果(键<阵列[MID])应该是中旬 - 1)。非常感谢大家!我添加了修复程序。
public class sortingSearching {
public static boolean linearSearch (int [] array, int key) {
for (int i = 0; i < array.length; i++) {
if (array [i] == key)
return true;
}// end for
return false;
}
public static int linearSearch2 (int [] array, int key) {
for (int i = 0; i < array.length; i++) {
if (array [i] == key)
return i;
}//end for
return -1;
}//end linearSearch2
public static boolean binarySearch (int [] array, int key) {
int left = 0;
int right = array.length - 1;
int mid = (left + right) /2;
while (left <= right) {
if (array[mid] == key)
return true;
else if (key < array[mid])
right = mid - 1;
else
left = mid + 1;
mid = (left + right) /2;
} //end while
return false;
}//end binarySearch
public static void printIntArray (int [] array) {
for (int i = 0; i < array.length; i++) {
if (i%10 == 0)
System.out.println();
System.out.print(array[i] + " ");
} // end for
}
public static void selectionSort (int [] array) {
for (int start = 0; start < array.length - 1; start ++) {
int minI = start;
for (int i = start + 1; i < array.length; i++)
if (array[i] < array[start])
minI = i;
int temp = array[start];
array[start] = array[minI];
array[minI] = temp;
}//end for
} //end selectionSort
public static void main (String args []) {
int [] array = new int [20];
for (int i =0; i < array.length; i++)
array[i] = (int)((Math.random() * 100) + 1);
//print the array using printArray
printIntArray(array);
System.out.println();
//use linearSearch to search for 30, 86, and 87
System.out.println("Searching for 30 in the random array. If true is returned, " +
"the value was found. If false was returned, the value was not found.");
System.out.println(linearSearch(array, 30));
System.out.println("Searching for 86 in the random array. If true is returned, " +
"the value was found. If false was returned, the value was not found.");
System.out.println(linearSearch(array, 86));
System.out.println("Searching for 87 in the random array. If true is returned, " +
"the value was found. If false was returned, the value was not found.");
System.out.println(linearSearch(array, 87));
//use linearSearch to locate the first occurrences of 25, 80, and 91
System.out.println("Searching for the location of 25 in the random array. If -1 is " +
"returned, the number was not found in the array.");
System.out.println(linearSearch2(array, 25));
System.out.println("Searching for the location of 80 in the random array. If -1 is " +
"returned, the number was not found in the array.");
System.out.println(linearSearch2(array, 80));
System.out.println("Searching for the location of 91 in the random array. If -1 is " +
"returned, the number was not found in the array.");
System.out.println(linearSearch2(array, 91));
//use selectionSort to sort the array
selectionSort(array);
//use binarySearch to search for 11, 28, 74, and 99
System.out.println("Searching for 11 in the random array using binary search. If true is returned, " +
"the value was found. If false was returned, the value was not found.");
System.out.println(binarySearch (array, 11));
System.out.println("Searching for 28 in the random array using binary search. If true is returned, " +
"the value was found. If false was returned, the value was not found.");
System.out.println(binarySearch (array, 28));
System.out.println("Searching for 74 in the random array using binary search. If true is returned, " +
"the value was found. If false was returned, the value was not found.");
System.out.println(binarySearch (array, 74));
System.out.println("Searching for 99 in the random array using binary search. If true is returned, " +
"the value was found. If false was returned, the value was not found.");
System.out.println(binarySearch (array, 99));
} //end main
} //end sortingSearching
另外,我很抱歉main方法中的所有打印语句都让人分心。我想把它们拿出来以方便阅读,但我希望它能像我一直在运行它一样。
它们不会返回任何内容,因为您没有将返回值分配给任何变量。
使此:
boolean foo= linearSearch(array, 86);
System.out.println(foo);
或
System.out.println(linearSearch(array, 86));
等。
我认为'linearSearch'返回一个布尔值:) – christopher 2013-05-09 20:01:00
linearSearch(array, 30);
他们确实会返回一些东西。但做一些与返回值!
boolean value = linearSearch(array, 30);
System.out.println(value);
或更简单:
System.out.println(linearSearch(array, 30));
在回答您的编辑
您需要启动left
到1
。你正在执行整数除法,它永远不会达到零。因此right
卡在1
和left
总是比它少。
+1对于确切时间 – 2013-05-09 19:59:37
克里斯,这是完美的。感谢您回复我的编辑。 – user2130057 2013-05-09 20:19:12
那么,如果这就是一切,一定要标记我的答案是正确的:) – christopher 2013-05-09 20:20:08
您必须将的排序/搜索方法调用的println()
语句,否则结果将不会被打印!就像这样:
System.out.println(
"Searching for 30 in the random array. If true is returned, " +
"the value was found. If false was returned, the value was not found." +
linearSearch(array, 30));
或者,将结果存储在一个局部变量 - 但是,你必须将变量传递给println()
:
boolean result = linearSearch(array, 30);
System.out.println(
"Searching for 30 in the random array. If true is returned, " +
"the value was found. If false was returned, the value was not found." +
result);
他们返回他们所应该,只是你选择不对他们返回的东西做任何事情。
您可以通过在System.out.println()
中包装函数调用或使用ret = yourfunction(params)
存储返回值并稍后显示ret
来解决此问题。
确保您的方法何时返回某物获得回报。 IE:'VARIABLE = binarySearch(array,74)' – Tdorno 2013-05-09 19:57:57
您是否调试过'binarySearch'函数? – christopher 2013-05-09 20:06:20
请检查我的编辑。 – christopher 2013-05-09 20:08:33