如何结束开关内部的开关。爪哇

发布于 2024-12-11 05:51:22 字数 1755 浏览 3 评论 0原文

我正在尝试在开关内做一个开关。我已经编译它了,但唯一的事情是,每次运行它时,我都会为第一个开关选择一个案例,一旦我为开关内部的第一个开关选择一个案例,它就会还要求我为第一个开关内的第二个开关选择一个案例,然后是第三个开关,然后结束。在选择开关内的第一个开关后,如何使整个程序重新从头开始,或者结束程序。谢谢。

前任。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好久不见√ 2024-12-18 05:51:23

只需在 switch 语句内的 switch 语句后面放置一个 break 即可。

即(例如)

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; 
        } 

break;  <--- this is the addition
case 2: //Blue 

或者(并且会使整个事情更具可读性)将这些开关放入另一个函数中

Just put a break after the switch statements that are inside the switch statements.

i.e. (for exampe)

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; 
        } 

break;  <--- this is the addition
case 2: //Blue 

Alternatively (and would make the whole thing more readable) put those switches into another function

淡水深流 2024-12-18 05:51:23

你在哪里使用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.

不寐倦长更 2024-12-18 05:51:23

您可以将任何开关包装在条件循环(while / do-while)内,当它到达末尾时,如果您的条件未达到(假设您希望变量 donetrue),切换将重复。

另外,您可以在开关中的打印函数之后使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文