[英]How do you restart this switch statement when the default case is accessed?
I am VERY new to programming... What can I do to make the switch statement start over when the default case is promted (for example when you enter "5"). 我对编程非常陌生...在默认情况下(例如,输入“ 5”时),如何使switch语句重新开始。 Any help would be great! 任何帮助将是巨大的!
I saw a similar question about this, but I couldn't use the answers. 我对此也有类似的疑问,但我无法使用答案。
int column= StdOut.println();
switch(column) {
case 0: StdOut.println("Good");
break;
case 1: StdOut.println("Ok");
break;
case 2: StdOut.println("Bad");
break;
default:
break;
}
Surround it in a loop and add a variable that breaks the loop when you reach something you like: 将其围绕在一个循环中,并添加一个变量,当您达到所需的效果时,该变量将中断循环:
int column= StdOut.println();
boolean isBad = true;
do{
switch(column) {
case 0: StdOut.println("Good");
isBad = false;
break;
case 1: StdOut.println("Ok");
isBad = false;
break;
case 2: StdOut.println("Bad");
isBad = false;
break;
default:
isBad = true;
break;
}
}while(isBad);
while(true) { .... switch (...) { case ....: .... break; // Exit switch statement, but not the loop // More cases here default: continue; // Go to the next iteration of the loop } break; // Exit the loop }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.