Java – While循环不起作用后的IF语句

谢谢你们,并且出现了新的问题。

即使列表和用户输入正确,它仍会打印出"Your movie or/and theatre cannot be found."

我发现了关于这个问题的事情。 如果列表中的任何一个(电影0-1项目和剧院0-1项目)中有1个项目,则不会打印出"Your movie or/and theatre cannot be found."

但是当其中任何一个(电影1项目和剧院2或电影2和剧院1)中有多个项目时,它将打印出if(found == false)语句。

 public void addScreening(){ System.out.println("-ADD NEW SCREENING-"); String mTitle = Helper.readString("Enter movie title > "); String tName = Helper.readString("Enter theatre name > "); boolean found = true; while(found == true){ for (int i = 0; i < movies.size(); i++) { for (int j = 0; j  "); int month = Helper.readInt("Enter month > "); int day = Helper.readInt("Enter day > "); int hour = Helper.readInt("Enter hour > "); int min = Helper.readInt("Enter min > "); screenings.add(new MovieScreening(Helper.thisDate(year, month, day, hour, min), movies.get(i),theatres.get(j), 0)); System.out.println("Added successfully"); }else if((!movies.get(i).getTitle().contains(mTitle) || !mTitle.contains(movies.get(i).getTitle())) || (!theatres.get(j).getName().contains(tName) || !tName.contains(theatres.get(j).getName()))){ found = false; } } }break; }if (found == false){ System.out.println("Your movie or/and theatre cannot be found."); found = true; } } 

产量

 -ADD NEW SCREENING- Enter movie title > 3 Enter theatre name > 3 Enter year > 3 Enter month > 3 Enter day > 3 Enter hour > 3 Enter min > 3 Added successfully Your movie or/and theatre cannot be found. 

简单地改变

 if (found = false) 

 if (found == false) //OR if (!found) 

您使用赋值运算符( = )而不是比较( == )。 这是一个常见的拼写错误,在许多情况下,最简单的方法是使用这些格式。

 if (found) {} // if (found == true) if (!found) {} // if (found == false) 

改变这个:

  if (found = false){ 

对此:

  if (found == false){