我想創建一個用於搜索書籍的系統。除了else語句外,我沒有任何問題。在情況1中,如果用戶輸入HarryPotter,則必須顯示它可用。如果用戶輸入了其他內容,程序必須顯示它不可用,但是當我編寫這樣的代碼時會出現錯誤。下面是代碼:如果else語句使用arraylist
ArrayList<String> EnglishCategorie = new ArrayList<String>();
EnglishCategorie.add("HarryPotter");
boolean repeatAgain = true;
while(repeatAgain) {
System.out.println("Please choose an option (between 1 and 5) :");
System.out.println("Press 1 to search a book in English Categorie.
System.out.println("Press 2 to exit the program. "); ");
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("Please enter a book name: ");
bookName = input.nextLine();
if(bookName == EnglishCategorie.contains("HarryPotter"))
System.out.println("It is available.");
else
System.out.println("It is not available.");
break;
有什麼問題?你說你的if語句有問題,但沒有說明問題是什麼。 – Carcigenicate
你已經錯過了大括號,切換if和else塊。你最終的代碼是一樣的嗎? – MrB
可以請你發佈錯誤信息嗎? – Chip