[英]While loop, a println is double printing
I have an infinite while loop in my code and the thing is if I call the getInput method ("input" case) or resize method ("resize" case, after it executes it prints the command line twice like this:我的代码中有一个无限的 while 循环,问题是如果我调用 getInput 方法(“input” case)或 resize 方法(“resize” case,在它执行后它会像这样打印命令行两次:
Enter one of the following commands: input, display, filter, resize or exit输入以下命令之一:输入、显示、过滤、调整大小或退出
Enter one of the following commands: input, display, filter, resize or exit输入以下命令之一:输入、显示、过滤、调整大小或退出
This is my code for the 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;
case "display":
displayContents(data);
break;
case "filter":
data = removeEvens(data);
break;
case "resize":
data = resize(data);
break;
case "exit":
System.exit(0);
}
The break within the switch breaks from switch.开关内的中断从开关中断。 Then the infinite loop continues.
然后无限循环继续。
If you want break from the loop, you could use a label.如果你想打破循环,你可以使用标签。
ie 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.