String searchValue;
boolean found = false;
int index = 0;
System.out.println("Enter a name to search for in the array.");
searchValue = kb.nextLine();
while (found == false && index < names.length) {
if (names[index].indexOf(searchValue) != -1) {
found = true;
} else {
index++;
}
}
if (found) {
System.out.println("That name matches the following element:");
System.out.println(names[index]);
} else {
System.out.println("That name was not found in the array.");
}
就像標題所說的那樣,這隻產生第一個匹配,而不是數組中的所有匹配。我將如何改變它以顯示所有匹配?Java - 搜索數組只產生第一個匹配並且不是全部匹配
因爲一旦找到匹配,該變量設置爲true,並且當條件不滿足從而跳過剩餘的比賽 – user1339772