2016-12-26 56 views
-3

我想創建一個用於搜索書籍的系統。除了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; 
+1

有什麼問題?你說你的if語句有問題,但沒有說明問題是什麼。 – Carcigenicate

+0

你已經錯過了大括號,切換if和else塊。你最終的代碼是一樣的嗎? – MrB

+0

可以請你發佈錯誤信息嗎? – Chip

回答

0

if(bookName == EnglishCategorie.contains("HarryPotter")) 

應該

if(EnglishCategorie.contains(bookName)) 

contains返回booleanbookNameString,並且是String(s)的Collection

+0

我做了你的建議,但現在程序顯示請輸入一個書名,它不會自動提供。 –

+0

@TarıkGöl這是一個不同的問題。現在,你有[這個問題](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo)。 –

+0

工作正常!非常感謝。 @Elliott Frisch –