所以我没有错误,在我的代码,但由于某种原因,如果数组包含它线性字符串数组的搜索
public static void linSrch(String[] names) {
Scanner inputDevice = new Scanner(System. in);
System.out.println("Please enter search");
String search;
search = inputDevice.nextLine();
boolean searchReturn;
for (int index = 0; index < names.length - 1; index++) {
if (names[index] == search) {
searchReturn = true;
}
}
if (searchReturn = true) {
System.out.println(search + " is found in the array.");
} else {
System.out.println(search + " not found in the array");
}
}
若要比较两个字符串是否相等,请执行'names [index] .equals(search)'而不是使用'==' –
另外,'if(searchReturn = true){'不检查是'searchReturn '是'true'。它**将值'true'赋予变量'searchReturn'。你只需要'if(searchReturn){...' – azurefrog