简体   繁体   中英

How can I loop back to a main menu in Java * beginner coder*

I am trying to loop back to the main menu using a while loop, and I cannot seem to get it to work. How can I implement this properly?

I've tried a do-while loop and a while and it's not working. I'm doing something wrong and I don't know what it is

boolean main_menu = true;
while (main_menu) {


    System.out.println("       Welcome to Zoos Victoria          ");
    System.out.println("        M A I N        M E N U           ");
    System.out.println(" Zoo has the following ticketing options:");
    System.out.println("         1 = Child (4-5 yrs)             ");
    System.out.println("         2 = Adult (18+ yrs)             ");
    System.out.println("      3 = Senior (60+ yrs) "  +  "\n"     );

    do {
    // select ticket option
    System.out.println(" Please enter your option:"     );
    ticket_option = input.nextInt();

        switch (ticket_option) {

            // Child_ticket
            case 1: 
                ticket_price = child_ticket; 
                ticket_name = " Child tickets";
                break; 

            // adult ticket
            case 2:  
                ticket_price = adult_ticket;
                ticket_name = " Adult tickets";
                break;

            //senior ticket 
            case 3:
                ticket_price = senior_ticket;
                ticket_name = " Senior tickets";
                break;  
            default:    
                System.out.println("Invalid entry!");
        } 

    } while( ticket_option > 3); //end of do while loop


    System.out.println("    Enter the number of tickets you would like:");
    ticket_amount = input.nextInt();
    total_price = ticket_amount * ticket_price;

    System.out.println(" you are purchasing " + ticket_amount + 
           ticket_name + " at " + "$"+ticket_price  + " each!");

    System.out.println(" Press 1 to confirm purchase"); 
    confirm_purchase = input.nextInt();

    if (confirm_purchase == 1)
        System.out.println("Total amount payable: "+ "$"+total_price);  
    else {  
        System.out.println("invalid key!");           
        main_menu = false;
    }   
      if (confirm_purchase == 1 ) {
System.out.println("Total amount payable: "+ "$"+total_price);  
    main_menu = false;
} else {            
        System.out.println("invalid key!");
        }

   System.out.println("Would you like to go back to the main menu?(Y/N) ");
   to_continue = input.next().toUpperCase();

   if (to_continue.equals("Y")){
     // back to main menu here
       System.out.println("Back to the main menu");

   }  else {
       System.out.println("Total amount payable: "+ "$"+total_price);
       System.out.println("Have a great time!");


   }
}// end while loop

} }

In the if statement the user is to press 1, if they don't, the program is to go back to the main menu. This currently does not work properly.

In your condition for confirm purchase, if user input an invalid key, you are putting the main_menu to false. you should remove it.

if (confirm_purchase == 1) {
System.out.println("Total amount payable: "+ "$"+total_price);  
} else {  
      System.out.println("invalid key!");           
             // main_menu = false; 
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM