Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 7 years ago

How to return to main menu from submenu?

I'm trying to get out of the sub-menu loops after a user presses 4, and return to the main menu. How do i do this?? Here is my code:

import java.util.*;

import java.math.*;

public class

{

/**

*

*

*

*

*

*/

public static void main(String[] args)

{

Scanner keyboard = new Scanner(System.in);

int userChoice = 0;

int mexChoice = 0;

int mexAmount = 0;

int chiChoice = 0;

int chiAmount = 0;

int gerChoice = 0;

int gerAmount = 0;

int ffChoice = 0;

int ffAmount = 0;

int returnChoice = 4;

double chineseAmount = 0;

double bc = 6.50;

double ss = 7.50;

double bb = 8.00;

double mexicanAmount = 0;

double mp = 5.00;

double tp = 6.00;

double ep = 7.00;

double germanAmount = 0;

double ws = 10.00;

double hb = 12.00;

double bp = 9.00;

double fastfoodAmount = 0;

double ham = 4.50;

double chs = 5.00;

double hd = 3.50;

{

System.out.println("");

System.out.println("\t\t\t\t\tF O O D M E N U\n\n\n " );

System.out.println("\t\t\t1.) Mexican Food ");

System.out.println("\n\t\t\t2.) Chinese Food ");

System.out.println("\n\t\t\t3.) German Food ");

System.out.println("\n\t\t\t4.) Fast Food ");

System.out.println("\n\t\t\t5.) Exit Menu");

System.out.print("\n\n\t\t\t\tMake your choice (1, 2, 3, 4, or 5): ");

userChoice = keyboard.nextInt();

if (userChoice == 5)

{

System.out.printf("\n\n\t\t\t\t\t G O O D B Y E!");

}

while (userChoice == 1)

{

{

System.out.println("\n\n\t\t\tM E X I C A N:");

System.out.println("\t\t\t\t\t1.) Mexican Plate\t\t$5.00 ");

System.out.println("\t\t\t\t\t2.) Taco Plate\t\t\t6.00");

System.out.println("\t\t\t\t\t3.) Enchilada Plate\t\t7.00");

System.out.println("\t\t\t\t\t4.) EXIT");

System.out.print("\t\t\tPlease enter the menu item you want (1, 2, 3, or 4 to EXIT):");

mexChoice = keyboard.nextInt();

if (mexChoice == 4)

{

System.out.printf("\n\n\t\t\t\t\t G O O D B Y E!");

break;

}

System.out.print("\n\t\t\tHow many orders of this item do you want?");

mexAmount = keyboard.nextInt();

}

if (mexChoice == 1)

{

mexicanAmount = mp * mexAmount;

}

if (mexChoice == 2)

{

mexicanAmount = tp * mexAmount;

}

if (mexChoice == 3)

{

mexicanAmount = ep * mexAmount;

}

System.out.printf("\n\n\t Your Total is: $%,10.2f\t\t", mexicanAmount);

System.out.print("\n\n\t\t\tEnter 4 to return to the Main Menu: ");

returnChoice = keyboard.nextInt();

}

while (userChoice == 2)

{

{

System.out.println("\n\n\t\t\tC H I N E S E:");

System.out.println("\t\t\t\t\t1.) Bourbon Chicken\t\t$6.50 ");

System.out.println("\t\t\t\t\t2.) Sweet and Sour Chicken\t7.50");

System.out.println("\t\t\t\t\t3.) Broccoli Beef\t\t8.00");

System.out.println("\t\t\t\t\t4.) EXIT");

System.out.print("\n\t\t\tPlease enter the menu item you want (1, 2, 3, or 4 to EXIT):");

chiChoice = keyboard.nextInt();

if (chiChoice == 4)

{

System.out.printf("\n\n\t\t\t\t\t G O O D B Y E!");

break;

}

}

System.out.print("\n\t\t\tHow many orders of this item do you want?");

chiAmount = keyboard.nextInt();

if (chiChoice == 1)

{

chineseAmount = bc * chiAmount;

}

if (chiChoice == 2)

{

chineseAmount = ss * chiAmount;

}

if (chiChoice == 3)

{

chineseAmount = bb * chiAmount;

}

System.out.printf("\n\n\t Your Total is: $%,10.2f\t\t", chineseAmount);

System.out.print("\n\n\t\t\tEnter 4 to return to the Main Menu: ");

returnChoice = keyboard.nextInt();

}

2 Answers

Relevance
  • 7 years ago
    Favorite Answer

    Simple, put it in another loop! Add statements that perform a break after return choice == 4.

    import java.util.*;

    import java.math.*;

    public class FoodMenu

    {

    /**

    *

    *

    *

    *

    *

    */

    public static void main(String[] args)

    {

    Scanner keyboard = new Scanner(System.in);

    int userChoice = 0;

    int mexChoice = 0;

    int mexAmount = 0;

    int chiChoice = 0;

    int chiAmount = 0;

    int gerChoice = 0;

    int gerAmount = 0;

    int ffChoice = 0;

    int ffAmount = 0;

    int returnChoice = 4;

    double chineseAmount = 0;

    double bc = 6.50;

    double ss = 7.50;

    double bb = 8.00;

    double mexicanAmount = 0;

    double mp = 5.00;

    double tp = 6.00;

    double ep = 7.00;

    double germanAmount = 0;

    double ws = 10.00;

    double hb = 12.00;

    double bp = 9.00;

    double fastfoodAmount = 0;

    double ham = 4.50;

    double chs = 5.00;

    double hd = 3.50;

    while(true){

    System.out.println("");

    System.out.println("\t\t\t\t\tF O O D M E N U\n\n\n " );

    System.out.println("\t\t\t1.) Mexican Food ");

    System.out.println("\n\t\t\t2.) Chinese Food ");

    System.out.println("\n\t\t\t3.) German Food ");

    System.out.println("\n\t\t\t4.) Fast Food ");

    System.out.println("\n\t\t\t5.) Exit Menu");

    System.out.print("\n\n\t\t\t\tMake your choice (1, 2, 3, 4, or 5): ");

    userChoice = keyboard.nextInt();

    keyboard.nextLine();

    if (userChoice == 5)

    {

    System.out.printf("\n\n\t\t\t\t\t G O O D B Y E!");

    break;

    }

    while (userChoice == 1)

    {

    {

    System.out.println("\n\n\t\t\tM E X I C A N:");

    System.out.println("\t\t\t\t\t1.) Mexican Plate\t\t$5.00 ");

    System.out.println("\t\t\t\t\t2.) Taco Plate\t\t\t6.00");

    System.out.println("\t\t\t\t\t3.) Enchilada Plate\t\t7.00");

    System.out.println("\t\t\t\t\t4.) EXIT");

    System.out.print("\t\t\tPlease enter the menu item you want (1, 2, 3, or 4 to EXIT):");

    mexChoice = keyboard.nextInt();

    if (mexChoice == 4)

    {

    System.out.printf("\n\n\t\t\t\t\t G O O D B Y E!");

    break;

    }

    System.out.print("\n\t\t\tHow many orders of this item do you want?");

    mexAmount = keyboard.nextInt();

    }

    if (mexChoice == 1)

    {

    mexicanAmount = mp * mexAmount;

    }

    if (mexChoice == 2)

    {

    mexicanAmount = tp * mexAmount;

    }

    if (mexChoice == 3)

    {

    mexicanAmount = ep * mexAmount;

    }

    if(mexChoice == 4){

    break;

    }

    System.out.printf("\n\n\t Your Total is: $%,10.2f\t\t", mexicanAmount);

    System.out.print("\n\n\t\t\tEnter 4 to return to the Main Menu: ");

    returnChoice = keyboard.nextInt();

    if(returnChoice == 4){

    break;

    }

    }

    while (userChoice == 2)

    {

    {

    System.out.println("\n\n\t\t\tC H I N E S E:");

    System.out.println("\t\t\t\t\t1.) Bourbon Chicken\t\t$6.50 ");

    System.out.println("\t\t\t\t\t2.) Sweet and Sour Chicken\t7.50");

    System.out.println("\t\t\t\t\t3.) Broccoli Beef\t\t8.00");

    System.out.println("\t\t\t\t\t4.) EXIT");

    System.out.print("\n\t\t\tPlease enter the menu item you want (1, 2, 3, or 4 to EXIT):");

    chiChoice = keyboard.nextInt();

    if (chiChoice == 4)

    {

    System.out.printf("\n\n\t\t\t\t\t G O O D B Y E!");

    break;

    }

    }

    System.out.print("\n\t\t\tHow many orders of this item do you want?");

    chiAmount = keyboard.nextInt();

    if (chiChoice == 1)

    {

    chineseAmount = bc * chiAmount;

    }

    if (chiChoice == 2)

    {

    chineseAmount = ss * chiAmount;

    }

    if (chiChoice == 3)

    {

    chineseAmount = bb * chiAmount;

    }

    if(chiChoice == 4){

    break;

    }

    System.out.printf("\n\n\t Your Total is: $%,10.2f\t\t", chineseAmount);

    System.out.print("\n\n\t\t\tEnter 4 to return to the Main Menu: ");

    returnChoice = keyboard.nextInt();

    if(returnChoice == 4){

    break;

    }

    }

    }

    }

    }

  • 4 years ago

    The alternative command doesn't exist on XP, so Jake's resolution won't work on XP, but it surely's a better alternative for Vista or later, as choice will not receive any entries however the ones provided. Use Lawrence's solution, however add ought to add one line, goto start. In the event that they variety some thing as opposed to the choices you list, it is going to fall through and run submenu1 which you could add the /I change to the If assertion to make them case insensitive, so they can form Y or y if /I "%menu_choice%"=="Y" goto submenu_1 : CLS ECHO Welcome, %USERNAME%! Echo. Echo What would you find irresistible to do? Echo. Echo 1. Show Submenu 1 echo 2. Display Submenu 2 echo 3. Display Submenu 3 echo four. Exit set /p option="Enter your choice: " if "%alternative%"=="1" goto submenu_1 if "%option%"=="2" goto submenu_2 if "%alternative%"=="three" goto submenu_3 if "%option%"=="four" goto :EOF Goto start

Still have questions? Get your answers by asking now.