2016-12-26 58 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 –