【发布时间】:2014-01-25 21:09:12
【问题描述】:
谢谢大家,新问题出现了。
即使列表和用户输入正确,它仍然会打印出"Your movie or/and theatre cannot be found."
我发现的有关该问题的信息。当任一列表中有 1 项(电影 0-1 项和剧院 0-1 项)时,将不会打印出 "Your movie or/and theatre cannot be found."
但当其中任何一项(电影 1 项和剧院 2 或电影 2 和剧院 1)中的任何一项超过 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 < 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)应该是if (found == false)或者更好的if (!found) -
=分配,==比较。 -
永远不要将布尔值与
true或false进行比较。照原样使用它。 -
但这不是错误的原因。
-
如果这个条件也不满足,也许你应该在某个地方再次执行
found = false:} 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; }
标签: java if-statement