Java - While循环不起作用后的IF语句 [英] Java - IF statement after While loop not working
问题描述
谢谢大家,并且出现了新的问题。
Thanks guys, and, new problem arises.
即使列表和用户输入正确,它仍会打印出来你的找不到电影或/和剧院。
Even if the list and user input is correct, it will still print out "Your movie or/and theatre cannot be found."
我发现了有关问题的事情。当列表中的任何一个(电影0-1项目和剧院0-1项目)中有1个项目时,它将无法打印出来无法找到您的电影或/和影院。
Things that I have found out about the problem. When there is 1 item in either one of the list (movie 0-1 item & theatre 0-1 item), it will not print out "Your movie or/and theatre cannot be found."
但是当其中任何一个中有超过1个项目时(电影1项目和剧院2或电影2&剧院1) ,它将打印出 if(found == false)
语句。
But when there is more than 1 item in either 1 of them (movie 1 item & theatre 2 or movie 2 & theatre 1), it will print out the if(found == false)
statement.
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 < theatres.size(); j++) {
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()))) {
int year = Helper.readInt("Enter year > ");
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)
to
if (found == false)
//OR
if (!found)
您使用的是赋值运算符( =
)而不是比较( ==
)。这是一个常见的拼写错误,在很多情况下最容易使用这些格式。
You were using assignment operator (=
) instead of comparison (==
). This is a common typo and in many cases it is easiest to use these formats.
if (found) {} // if (found == true)
if (!found) {} // if (found == false)
这篇关于Java - While循环不起作用后的IF语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!