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("");
}
}
}
Just put a
breakafter 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