Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中的Do..While循环中显示错误消息_Java_Loops_Message_Do While - Fatal编程技术网

在java中的Do..While循环中显示错误消息

在java中的Do..While循环中显示错误消息,java,loops,message,do-while,Java,Loops,Message,Do While,我正在尝试让我的Do..While循环显示一条错误消息,说明它为什么再次请求用户输入。这是我的代码: do { System.out.println("\nPlease enter the floor you are on: "); current_Floor = in.nextInt(); } // Loop until user input is equal to or less than the maximum floors while( current_Floor

我正在尝试让我的Do..While循环显示一条错误消息,说明它为什么再次请求用户输入。这是我的代码:

do {
    System.out.println("\nPlease enter the floor you are on: ");
    current_Floor = in.nextInt();   
}

// Loop until user input is equal to or less than the maximum floors
while( current_Floor > MAX_FLOORS );{
    System.out.println("Please enter a floor which is less than 8");
}

此代码的问题在于,我添加的错误消息在输入正确的用户输入后出现,而我需要在添加用户输入之前显示它。有什么想法吗?

请查看do while循环的流程图:

在Java中,while循环不是这样工作的。我建议你坚持做一个while循环,因为你的第一次传球和之后的每一次都有点不同

System.out.println("Please enter a floor which is less than 8");
current_Floor = in.nextInt();

while (current_Floor > MAX_FLOORS) {
    System.out.println("\nPlease enter the floor you are on: ");
    current_Floor = in.nextInt();    
}

这里提示用户输入。如果无效,则进入显示错误消息的循环并再次提示。这将一直持续到用户行为。

,do-while不是这里使用的最好的循环类型,因为您需要在重复部分的中间检查条件,而不是在结束时。可以用do while执行此操作,但最终会检查两次情况

为了不重复代码,有一个很好的替代方案。第一行意味着永远循环,但实际上它只会循环直到遇到中断,这是在用户输入正确的输入时发生的

for(;;) {
    System.out.println("\nPlease enter the floor you are on: ");
    current_Floor = in.nextInt();   
    if (current_Floor <= MAX_FLOORS) {
        break;
    }

    System.out.println("Please enter a floor which is less than 8");
}

Java do while语法如下所示:

do {
    //block of code to execute
} while([some boolean condition]);

您的问题是您误用了do-while循环

do{
    ...STUFF...
}while( $CONDITION );
...MORE STUFF...
是这样工作的

做事 计算条件 如果条件为真,则转到1,如果不是,则转到4 多做事 在您的情况下,STUFF是要求用户输入楼层,条件是用户选择的楼层是否大于最大楼层?,更多STUFF是要求用户输入小于8的楼层


您所做的可能是让条件一直询问,直到选择了有效的楼层,并在do while循环中签入if语句。

您的格式设置很糟糕,这可能是让您感到困惑的地方。这个在你的while结束时,dowhile结束。之后的大括号(包含错误语句)只是一组静态大括号:它们与循环没有任何关联

do{
    ...STUFF...
}while( $CONDITION );
...MORE STUFF...
如果您将代码的格式设置得更清晰,它看起来是这样的,这表明您的小于8的消息放置不正确,并且在退出循环后将始终打印出来

do {
    System.out.println("\nPlease enter the floor you are on: ");
    current_Floor = in.nextInt();   
} while( current_Floor > MAX_FLOORS );

{ // Useless braces!
    System.out.println("Please enter a floor which is less than 8");
} // Useless braces!
如果在通知用户迭代失败时希望循环直到满足某个条件,通常的习惯用法是永远循环,然后在满足条件时中断:

while (true) {
    System.out.println("\nPlease enter the floor you are on: ");
    current_Floor = in.nextInt();   
    if (current_Floor <= MAX_FLOORS) {
        break;
    }

    // This prints only if the user entered a bad number.
    System.out.println("Please enter a floor which is less than 8");
}
顺便说一句,代码中提到的最大楼层数有点令人讨厌,而将魔法数字8硬编码到错误消息中:最好使用一些来告诉用户实际的最大楼层数是多少

System.out.println(String.format("Please enter a floor which is less than %d", MAX_FLOORS+1));

另一方面,您的命名方案有点奇怪:按照惯例,Java中的大多数变量名都应该使用camelCase,没有下划线,例如currentFloor。不过,您的MAX_FLOORS还可以,因为它似乎是一个类似于静态/枚举的变量,通常使用带下划线的capslock。

此结构不是do whileSo?OP已经在遵循这种结构。是的……但她或他还有更多……希望这将推动他/她朝着正确的方向前进,而不只是给出答案。即使for循环更好,当一个新程序员正在学习时,他们需要知道如何编程do-while循环。问题是有什么想法吗。它并没有说我必须使用do-while循环。我回答了问题。你可能有道理。我重新阅读了这个问题,对我来说,任何想法都是指为什么它不起作用,而不是如何让它以其他方式发挥作用,他们是否需要使用do-while,这有点离题了。他们显然有一个很大的误解,这是值得解决的。此外,在if语句中使用与条件相反的简单while循环比使用无限for循环的糟糕做法要好。@gravityplanx谁试图用最少的行数编写代码?不是我!我完全同意可读性。我主张不要重复一个条件。您希望复制条件本身或显示错误的行。我认为你还没有完全考虑清楚这一点。你可能会从亨利·凯特的答案中的第二个灰色方框中学到一些东西。他跟我解释为什么要满足OP的要求,最好的解决办法是有一个循环,中间休息。好的,谢谢哥们,我只学了20个星期的java,所以还是初学者,谢谢你的建议。