【问题标题】:Java - IF statement after While loop not working [closed]Java - While循环后的IF语句不起作用[关闭]
【发布时间】: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)
  • = 分配,== 比较。
  • 永远不要将布尔值与truefalse 进行比较。照原样使用它。
  • 但这不是错误的原因。
  • 如果这个条件也不满足,也许你应该在某个地方再次执行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


【解决方案1】:

简单的改变

if (found = false)

if (found == false)

//OR

if (!found)

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

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

【讨论】:

    【解决方案2】:

    改变这个:

      if (found = false){
    

    到这里:

      if (found == false){
    

    【讨论】:

    • 不行,改成if (!found) {。将布尔值与 truefalse 进行比较总是在自找麻烦。
    • 什么?这是荒谬的。比较 == false 不是否定逻辑。
    • 这是多余的,从帖子中可以看出,它增加了出错的可能性。在将零视为假而将非零视为真的语言中,像if (var == true) 这样的比较往往会产生难以察觉的意外行为。将布尔值与truefalse 进行比较充其量是不好的做法。如果您因为某些尚未确诊的精神疾病而绝对必须这样做,请使用 Yoda 条件来保护自己免受错误。
    • 根据我的经验,这是您的意见,在处理由具有“不合格”或“不活动”之类名称的业务人员设计的数据库的现实世界时,我看到的逻辑我没有t 写但必须支持 "if (Ineligible != false)" 与 "if (Ineligible == true)" 的正逻辑相比,甚至 "if Ineligible == false" 比 "if (!不合格)”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    相关资源
    最近更新 更多