【问题标题】:Checking While Loop with if statement使用 if 语句检查 While 循环
【发布时间】:2015-12-08 17:52:19
【问题描述】:

while 循环和 if 语句在第一次迭代中工作。完成后,它会打印“你想要另一个时间吗?”我输入“Y”程序终止,我不知道为什么。我的逻辑和语法似乎是正确的,但总是有我无法亲眼看到的缺陷。如果有人能指出缺陷,我将不胜感激。

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;
    }
}

【问题讨论】:

    标签: java if-statement while-loop


    【解决方案1】:

    您不应该检查缓冲区中是否还有另一行;您可以直接检查通过str 返回的输入。您第一次退出循环的可能原因是 in.hasNextLine() 为假。

    因此,这意味着您将 if 语句更改为:

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

    【讨论】:

    • 看我不知道。感谢您的帮助
    【解决方案2】:

    这就是你要找的东西。

    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;   
    
    }
    

    我改变的是,我在输入后比较 str 。 如果它是“Y”,它将继续,否则它将退出 while 循环。

    【讨论】:

    • 尼特:你可以说check = str.equals("Y");
    • 是的,我只是想解释一下。
    • @Makoto 对不起,“Nit”是什么意思? (所以不是英语母语,想知道我以后是否还会看到这个)
    • @Frakcool: "Nit" 如"nitpick"。只是指出我看到的一个错误。
    • @Makoto 我不会说这是一个错误,而是一种更好的编码方式。
    【解决方案3】:

    在您已经阅读之后,您正在尝试查看是否有下一行 (in.hasNextLine())。

    试试这个方法:

           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;
                }
            }
    

    好的,这里有一个更好的方法来完成整个事情:

    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;
            }
        }
    

    【讨论】:

    • 这比所需的要复杂得多。
    • @Makoto 绝对是,但这是 OP 所拥有的,我会尽快建议一个更好的循环。
    猜你喜欢
    • 2015-05-19
    • 2013-06-09
    • 2014-01-21
    • 2015-11-08
    • 2016-01-12
    • 2015-07-06
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多