简体   繁体   English

使用if语句检查While循环

[英]Checking While Loop with if statement

The while loop and if statement work through the first iteration. while循环和if语句在第一次迭代中起作用。 After it completes and it prints "Do you want another time?" 完成后,它会显示“您是否要再来一次?” and I input "Y" the program terminates and I don't know why. 我输入“ Y”,程序终止,我不知道为什么。 My logic and syntax seem to be correct but there is always flaws that I cant see with my own eyes. 我的逻辑和语法似乎是正确的,但总是有肉眼看不到的缺陷。 I would appreciate it if someone can point the flaw out. 如果有人可以指出缺陷,我将不胜感激。

while (!check) {
    str = in.nextLine();
    if (in.hasNextLine() && str.equals("Y")) {
        hours = ran.nextInt(11) + 1;
        minutes = ran.nextInt(60);
        if (minutes < 10) {
            System.out.println(hours + ":" + "0" + minutes);
        } else {
            System.out.println(hours + ":" + minutes);
        }
        System.out.println("Do you want another time?");

    } else {
        check = true;
    }
}

You shouldn't be checking to see if you have another line in the buffer; 您不应该检查缓冲区中是否还有另一行。 you can just explicitly check the input you get back through str instead. 您可以明确地检查通过str返回的输入。 The likely reason you're exiting out of the loop the first time is due to in.hasNextLine() being false. 您第一次退出循环的可能原因是in.hasNextLine()为false。

So, this means that you'd change your if statement to: 因此,这意味着您可以将if语句更改为:

if(str.equals("Y")) {

}

You are trying to see if there is a next line ( in.hasNextLine() ), after you have already read it. 你是想看看是否有下一个线( in.hasNextLine()你都已经后阅读。

Try this way: 尝试这种方式:

       while (!check) {
            if(in.hasNextLine()){
                System.out.println("Do you want another time?");
                str = in.nextLine();
                if("Y".equals(str)){
                    hours = ran.nextInt(11) + 1;
                    minutes = ran.nextInt(60);
                    if (minutes < 10) {
                        System.out.println(hours + ":" + "0" + minutes);
                    } else {
                        System.out.println(hours + ":" + minutes);
                    }
                    System.out.println("Do you want another time?");
                }
            }else{
                check = true;
            }
        }

Ok, here's a nicer way to do this whole thing: 好的,这是完成这件事的更好的方法:

while (in.hasNextLine()) {
        System.out.println("Do you want another time?");
        if("Y".equals(in.nextLine())){
            //do what you want to do
        }else{
          //perhaps you want to break, or whatever you want to do
          break;
        }
    }

Here Is what you looking for. 这就是您想要的。

while (!check) {        

        hours = ran.nextInt(11) + 1;
        minutes = ran.nextInt(60);

        if (minutes < 10) {
            System.out.println(hours + ":" + "0" + minutes);
        } 
        else {
            System.out.println(hours + ":" + minutes);
        }

        System.out.println("Do you want another time?");
        str = in.nextLine();

        if(str.equals("Y"))
          check = false;
        else
        check = true;   

}

What I changed is, I am comparing the str after taking the input. 我更改的是,在输入之后,我正在比较str。 if its "Y" it will continue otherwise it will go out of the while loop. 如果它的“ Y”将继续,否则它将退出while循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM