所以我沒有錯誤,在我的代碼,但由於某種原因,如果數組包含它線性字符串數組的搜索
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