Tai Tai - 1 month ago 6
Java Question

How do you go back to the previous menu with do while and switch?

I have a do-while loop within a do-while loop and switch cases for each of them.

I want the main menu to be looping forever and want the main menu cases to go back to the main menu after some condition.

For example:

//main menu
do {
case 1:
do {
case 1:
case 2:
} while(something);
//go back to main menu
case 2:
case 3:
case 4:
} while(true);


How may I achieve this in Java code?

Below is my code for your reference (should you need it) :)

import java.util.Scanner;

public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

do {

System.out.println("[1] Priority Queue");
System.out.println("[2] 2D Array");

System.out.print("Please choose a data structure: ");

int num = scanner.nextInt();
switch (num) {

// ==================== PriorityQueue ====================
case 1:
System.out.println("\n=== Priority Queue Test ===\n");

System.out.print("Enter size of priority queue: ");
PriorityQueue pq = new PriorityQueue(scanner.nextInt());

char a;
/* Perform Priority Queue operations */
do {
System.out.println("\nPriority Queue Operations\n");
System.out.println("1. insert");
System.out.println("2. remove");
System.out.println("3. check empty");

int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter job name and priority: ");
pq.insert(scanner.next(), scanner.nextInt());
break;
case 2:
System.out.println("\nJob removed \n\n" + pq.remove());
break;
case 3:
System.out.println("\nEmpty Status: " + pq.isEmpty());
break;
default:
System.out.println("Wrong Entry \n ");
break;
}
System.out.println("\nDo you want to continue? (y or n) \n");
a = scanner.next().charAt(0);
} while (a == 'Y' || a == 'y' || a != 'N' || a != 'n');
break;

// ==================== 2D Array ====================
case 2:
TwoDimensionalArray array = new TwoDimensionalArray();

System.out.println("\n=== 2D Array Test ===\n");
System.out.println("Enter size of 2D Array: ");
int DIM = scanner.nextInt();
int[][] table = new int[DIM][DIM];

char b;
do {

System.out.println("1. insert [x][y] coordinate");
System.out.println("2. remove [x][y] coordinate");
System.out.println("3. empty");
System.out.println("4. print");

int choice = scanner.nextInt();
switch (choice) {
case 1:
array.insert(table, scanner);
break;
case 2:
array.remove(table, scanner);
break;
case 3:
array.empty(DIM);
break;
case 4:
array.print(DIM, table);
break;
}
System.out.println("\nDo you want to continue? (y or n) \n");
b = scanner.next().charAt(0);
} while (b == 'Y' || b == 'y' || b != 'N' || b != 'n');
break;
default:
System.out.println("Please choose a number on the list!\n");
}
while (num == 1 || num == 2 || num == 3 || num == 4 || num == 5 || num == 6 || num == 7 || num == 8 || num == 9
|| num == 10 || num == 11 || num == 12 || num == 13 || num == 14 || num == 15 || num == 16 || num == 17)
;
} while (true);
}
}

Answer

Take a look at your while conditions.

while (a == 'Y' || a == 'y' || a != 'N' || a != 'n');

This always evaluates to true and so you never the leave the inner loop.

You need to change it to

while ((a == 'Y' || a == 'y') && (a != 'N' || a != 'n'));

This part doesn't seem necessary

  while (num == 1 || num == 2 || num == 3 || num == 4 || num == 5 || num == 6 || num == 7 || num == 8 || num == 9
      || num == 10 || num == 11 || num == 12 || num == 13 || num == 14 || num == 15 || num == 16 || num == 17)
    ;

If you remove it and change the while conditions accordingly it should work.

Comments