![](/img/trans.png)
[英]System.out.println printing twice in each iteration of while loop
[英]While loop, a println is double printing
我的代码中有一个无限的 while 循环,问题是如果我调用 getInput 方法(“input” case)或 resize 方法(“resize” case,在它执行后它会像这样打印命令行两次:
输入以下命令之一:输入、显示、过滤、调整大小或退出
输入以下命令之一:输入、显示、过滤、调整大小或退出
这是我的循环代码:
while (true) {
System.out.println("\nEnter one of the following commands: input, display, filter, resize or exit");
String command = reader.nextLine();
switch (command) {
case "input":
data = getInput(data);
break;
case "display":
displayContents(data);
break;
case "filter":
data = removeEvens(data);
break;
case "resize":
data = resize(data);
break;
case "exit":
System.exit(0);
}
开关内的中断从开关中断。 然后无限循环继续。
如果你想打破循环,你可以使用标签。
IE
INPUT_LOOP:
while (true) {
System.out.println("\nEnter one of the following commands: input, display, filter, resize or exit");
String command = reader.nextLine();
switch (command) {
case "input":
data = getInput(data);
break INPUT_LOOP;
case "display":
displayContents(data);
break INPUT_LOOP;
case "filter":
data = removeEvens(data);
break INPUT_LOOP;
case "resize":
data = resize(data);
break INPUT_LOOP;
case "exit":
System.exit(0);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.