从java的子菜单返回主菜单

时间:2016-05-03 05:41:50

标签: java menu return

所以我当前的代码有一个问题,当我点击exit(4)时子菜单它没有返回到主菜单,它只是重复主菜单中的if语句while循环启动子菜单。我该如何解决这个问题?

import java.util.Scanner;
import java.util.*;
import java.util.ArrayList;

public class StarberksInterface
{


public static void main(String args[])
{

    Scanner console = new Scanner(System.in);
    store = new Store();
    String str, sName1, sName2, name;
    char c;
    int n=0;
    sName1 = "Callahan";
    sName2 = "Lambton";


    //This is the main menu that will be displayed first.
    System.out.println("       MAIN MENU FOR MANAGEMENT SYSTEM");
    System.out.println("===============================================");
    System.out.println("1. CHOOSE STORE");
    System.out.println("2. DISPLAY STORES");
    System.out.println("3. LOAD STORE VIA FILE");
    System.out.println("4. SAVE STORE TO FILE ");
    System.out.println("5. EXIT PROGRAM");
    System.out.println("===============================================");

    while(n!=5)// Exits the program when 4 is pressed
        {
            System.out.print("\n Please enter option 1-4 to continue...: ");
            n = Integer.parseInt(System.console().readLine());
            // Reads user input and takes them to selected code.
            if (n>5||n<1)
            {
                System.out.print("Invalid input, please try again...");
                continue;
            }
            if (n==1)// Takes to option 1 or sub menu
            {
                str="y";

                while(str.equals("y")||str.equals("Y"))
                {
                    System.out.println("Enter a store name [Callahan or Lambton] ");
                    name = console.next();
                    if (sName1.equals(name)|| sName2.equals(name))
                    {
                        StarberksInterface.subMenu();
                        return;
                    }
                    else 
                    {
                        System.out.println("There is no store under this name. Please try again.");
                    }

                }

            }
            if (n==2)// Gathers products in stores and displays the number of products
            {
                System.out.println(" Store data is being displayed.");
                System.out.println("===============================");
                System.out.println("Store: Callahan");
                System.out.println("       Number of products: "+store.getProductListSize());

            }
        }
}




public static void subMenu()
{
Scanner console = new Scanner(System.in);
String str;
char c;
int n=0;


        // this will be the sub menu that gets displayed.
        System.out.println("        INVENTORY MANAGEMENT SYSTEM");
        System.out.println("===============================================");
        System.out.println("1. ADD PRODUCT DATA");
        System.out.println("2. VIEW SINGLE PRODUCT DATA");
        System.out.println("3. DELETE PRODUCT");
        System.out.println("4. DISPLAY ALL PRODUCTS IN STORE");
        System.out.println("===============================================");
        System.out.println("5. EXIT SUB MENU");

        while(n!=5)// Exits the program when 4 is pressed
        {
            System.out.print("\n Please enter option 1-5 to continue...: ");
            n = Integer.parseInt(System.console().readLine());
            // Reads user input and takes them to selected code.
            if (n>5||n<1)
            {
                System.out.print("Invalid input, please try again...");
                continue;
            }
            if (n==1)// Takes to option 1 or addItem()
            {
                str="y";
                while(str.equals("y")||str.equals("Y"))
                {

                    StarberksInterface.addItem();
                    System.out.print("Would you like to enter another product ? (Y or N) : ");
                    str = console.next();
                }   
                continue;
            }
            if (n==2)// Takes to option 2 or prodData
            {
                str="y";
                while(str.equals("y")||str.equals("Y"))
                {
                    StarberksInterface.prodData();
                    System.out.println("\n***************************************************\n");
                    System.out.print("Stay viewing this page? (Y or N) ");
                    str = console.next();

                }
                continue;
            }

            if (n==3)// Takes to option 3 or delete item
            {
                System.out.print("Delete a product");
                continue;
            }

            if (n==4)// Takes to option 4 or view all products in store
            {
                System.out.print("Displaying all products in store");
                continue;
            }
        }
            if (product != null)// If there is information on the system
            // then the user will have the option to view data, before the program quits
            {
                System.out.println("\n***************************************************\n");
                System.out.println("\nAre you sure you want to quit? There is information stored on a product. ");
                System.out.println("\nWould you like to view if information? (Y / N) ");
                str="";
                str = console.next();
                while(str.equals("y")||str.equals("Y"))
                {

                    StarberksInterface.prodData();
                    return;
                }

            }

            else
            {
                System.out.print("\nThank you for using this inventory management software.\n");
                System.out.print("Developed by Xavier Edwards");
                System.out.println("\n***************************************************\n");
            }

3 个答案:

答案 0 :(得分:1)

更改

中的return语句
if (sName1.equals(name) || sName2.equals(name)) {
    StarberksInterface.subMenu();
    return;
}

if (sName1.equals(name) || sName2.equals(name)) {
    StarberksInterface.subMenu();
    break;
}

要再次打印菜单,您需要在循环内移动主菜单:

while (n != 5)// Exits the program when 4 is pressed{
    .....
}

答案 1 :(得分:0)

您需要在此循环中移动主菜单:

 while(n!=5)/
{
...
}

答案 2 :(得分:0)

尝试替换

n = Integer.parseInt(System.console().readLine());

通过

n = console.nextInt();

在两个地方