0
case 2:
System.out.println("Please enter Book ID: ");
String userinput2 = sc.next();
for (int i = 0; i < myBooks.size(); i++) {
if (myBooks.get(i).getBookID().contains(userinput2)) {
System.out.println("BookID: " + myBooks.get(i).getBookID() + "\nTitle: "
+ myBooks.get(i).getTitle() + "\nAuthor: " + myBooks.get(i).getAuthor());
System.out.println("Please enter new details of book");
System.out.println("Title: ");
String userinput7 = sc.next();
myBooks.get(i).setTitle(userinput7);
System.out.println("Author: ");
String u1 = sc.next();
myBooks.get(i).setAuthor(u1);
myBooks.get(i).setOnLoan(false);
myBooks.get(i).setNumLoans(0);
System.out.println("---------------------------------------------------");
System.out.println("The book has been successfully updated");
System.out.println("Book ID: " + myBooks.get(i).getBookID() + " " + "\nTitle: "
+ myBooks.get(i).getTitle() + " " + "\nAuthor: " + myBooks.get(i).getAuthor());
System.out.println("---------------------------------------------------");
}
else { System.out.println("Please enter a correct bookID");
}
}
//我的驗證檢查有問題。如果用戶輸入不存在的bookID,而不是打印出「請輸入正確的bookID」一次,它將打印出4次,這相當於我在數組列表中的對象數量。有沒有辦法對此進行分類?行代碼打印dublicates
考慮提供一個[可運行示例](HTTPS ://stackoverflow.com/help/mcve)它演示你的問題。這不是代碼轉儲,而是您正在做的事情的一個例子,它突出了您遇到的問題。這會減少混淆和更好的反應。 – MadProgrammer
在添加任何新項目之前,您應該首先檢查重複項 – MadProgrammer
並且在else執行後您也不允許用戶再次輸入,因此它會一直執行直到循環結束。嘗試在'else'塊中添加'userinput2 = sc.next();'或許。 –