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

时间:2014-01-25 21:09:12

标签: java if-statement

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

即使列表和用户输入正确,它仍会打印出"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)中有超过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.

2 个答案:

答案 0 :(得分:3)

只需更改

if (found = false)

if (found == false)

//OR

if (!found)

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

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

答案 1 :(得分:2)

改变这个:

  if (found = false){

到此:

  if (found == false){