如何结束开关内部的开关。爪哇
我正在尝试在开关内做一个开关。我已经编译它了,但唯一的事情是,每次运行它时,我都会为第一个开关选择一个案例,一旦我为开关内部的第一个开关选择一个案例,它就会还要求我为第一个开关内的第二个开关选择一个案例,然后是第三个开关,然后结束。在选择开关内的第一个开关后,如何使整个程序重新从头开始,或者结束程序。谢谢。
前任。
System.out.println("Pick a color.\n");
System.out.println(" 1. Red");
System.out.println(" 2. Blue");
System.out.println(" 3. Yellow");
System.out.println(" 4. Green");
Scanner kbReader = new Scanner(System.in);
int color = kbReader.nextInt();
System.out.println("The color you chose was " + color + ".");
String s1 = kbReader.nextLine();
switch (color)
{
case 1: //Red
System.out.println("Now enter an integer from 1 through 10.");
int num1 = kbReader.nextInt();
switch (num1)
{
case 1:
case 3:
case 5:
case 7:
case 9:
System.out.println("");
case 2:
case 4:
case 6:
case 8:
case 10:
System.out.println("");
break;
}
case 2: //Blue
System.out.println("Now enter an integer from 1 through 10.");
int num2 = kbReader.nextInt();
switch (num2)
{
case 1:
case 3:
case 5:
case 7:
case 9:
System.out.println("");
case 2:
case 4:
case 6:
case 8:
case 10:
System.out.println("");
}
case 3: //Yellow
System.out.println("Now enter an integer from 1 through 10.");
int num3 = kbReader.nextInt();
switch (num3)
{
case 1:
case 3:
case 5:
case 7:
case 9:
System.out.println("");
case 2:
case 4:
case 6:
case 8:
case 10:
System.out.println("");
}
case 4: //Green
System.out.println("Now enter an integer from 1 through 10.");
int num4 = kbReader.nextInt();
switch (num4)
{
case 1:
case 3:
case 5:
case 7:
case 9:
System.out.println("");
case 2:
case 4:
case 6:
case 8:
case 10:
System.out.println("");
}
}
}
I am trying to do a switch inside of a switch. Ive gotten it to compile, but the only thing is, every time I run it, I select one of the cases for the first switch, and once I go and select one of the cases for the first switch inside of the switch, it then also asks for me to select a case for the second switch inside of the first switch, then the third and forth, then it ends. How do I either make the entire program start again back at the beginning after selecting the first switch inside of the switch, or end the program. Thanks.
ex.
System.out.println("Pick a color.\n");
System.out.println(" 1. Red");
System.out.println(" 2. Blue");
System.out.println(" 3. Yellow");
System.out.println(" 4. Green");
Scanner kbReader = new Scanner(System.in);
int color = kbReader.nextInt();
System.out.println("The color you chose was " + color + ".");
String s1 = kbReader.nextLine();
switch (color)
{
case 1: //Red
System.out.println("Now enter an integer from 1 through 10.");
int num1 = kbReader.nextInt();
switch (num1)
{
case 1:
case 3:
case 5:
case 7:
case 9:
System.out.println("");
case 2:
case 4:
case 6:
case 8:
case 10:
System.out.println("");
break;
}
case 2: //Blue
System.out.println("Now enter an integer from 1 through 10.");
int num2 = kbReader.nextInt();
switch (num2)
{
case 1:
case 3:
case 5:
case 7:
case 9:
System.out.println("");
case 2:
case 4:
case 6:
case 8:
case 10:
System.out.println("");
}
case 3: //Yellow
System.out.println("Now enter an integer from 1 through 10.");
int num3 = kbReader.nextInt();
switch (num3)
{
case 1:
case 3:
case 5:
case 7:
case 9:
System.out.println("");
case 2:
case 4:
case 6:
case 8:
case 10:
System.out.println("");
}
case 4: //Green
System.out.println("Now enter an integer from 1 through 10.");
int num4 = kbReader.nextInt();
switch (num4)
{
case 1:
case 3:
case 5:
case 7:
case 9:
System.out.println("");
case 2:
case 4:
case 6:
case 8:
case 10:
System.out.println("");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需在 switch 语句内的 switch 语句后面放置一个
break
即可。即(例如)
或者(并且会使整个事情更具可读性)将这些开关放入另一个函数中
Just put a
break
after the switch statements that are inside the switch statements.i.e. (for exampe)
Alternatively (and would make the whole thing more readable) put those switches into another function
你在哪里使用break语句
你应该休息一下;您的案件执行完毕后的声明。
像这样。
案例1:
休息;
如果你不给出break语句,那么它将按顺序执行。在第一个开关之后,它将转到下一个开关,依此类推。
Where are you using break statement
you should give a break; statement after your case finish execution.
like this.
case 1:
break;
if you do not give break statement then it will execute sequentially .After first switch it will go to the next switch and so on.
您可以将任何开关包装在条件循环(while / do-while)内,当它到达末尾时,如果您的条件未达到(假设您希望变量 done 为 true),切换将重复。
另外,您可以在开关中的打印函数之后使用 break; 来停止继续开关选项。
要重复整个程序,请将其包装在 while 或 do-while 循环中。
You can either wrap any of the switches inside a conditional loop (while / do-while) and when it reaches the end, if you condition isn't achieved (say you want variable done to be true), the switch will repeat.
Also, you can use break; after the print functions in the switches to stop continuing the switch options.
To repeat the entire program, wrap it inside a while or do-while loop.