1

我有以下简单的主菜单,其中没有调用任何方法,当在每个菜单中选择退出选项时,我不知道如何使子菜单退出主菜单? 我是初学者,这个项目非常重要,好像我失败了,尽管已经支付了费用,但我将无法参加考试:(

菜单代码(到目前为止)如下:

*//菜单将包含 6 个列表,分别是:书籍、成员、员工、贷款、统计信息、退出。

//这些从 1 到 4 的列表将打开一个子菜单,其中包括:插入、搜索、删除、编辑、全部列出、退出。

//清单 5 将包括:前 5 本书、前 5 名成员、当月员工、过期列表、退出。

    import java.util.*;
    import java.io.*;
    public class Menu
      {
    public static void main(String args[]) throws IOException
      {
    Scanner sc = new Scanner(System.in);
    int option; 

    do 
    { 
    System.out.println("Main Menu:"); 
    System.out.println("1. Books"); 
    System.out.println("2. Members"); 
    System.out.println("3. Employees"); 
    System.out.println("4. Loans");
    System.out.println("5. Statistics");
    System.out.println("6. Exit");
    System.out.println("Enter your option [1,2,3,4,5,6]:");
    option = sc.nextInt();

    switch (option) {
        case 1: System.out.println("Books Sub-Menu:");
                System.out.println("1. Insert"); 
                System.out.println("2. Search"); 
                System.out.println("3. Delete"); 
                System.out.println("4. Edit");
                System.out.println("5. List All");
                System.out.println("6. Exit"); 
                System.out.println("Enter your option [1,2,3,4,5,6]:");
                option = sc.nextInt();

                switch (option){
                    case 1:
                    case 2: 
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    option = sc.nextInt();
                    break;
                }
        break;

        case 2: System.out.println("Members Sub-Menu:");
                System.out.println("1. Insert"); 
                System.out.println("2. Search"); 
                System.out.println("3. Delete"); 
                System.out.println("4. Edit");
                System.out.println("5. List All");
                System.out.println("6. Exit"); 
                System.out.println("Enter your option [1,2,3,4,5,6]:");
                option = sc.nextInt();

                 switch (option){
                    case 1: 
                    case 2: 
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    option = sc.nextInt();
                    break;
                }
        break;

        case 3: System.out.println("Employees Sub-Menu:");
                System.out.println("1. Insert"); 
                System.out.println("2. Search"); 
                System.out.println("3. Delete"); 
                System.out.println("4. Edit");
                System.out.println("5. List All");
                System.out.println("6. Exit"); 
                System.out.println("Enter your option [1,2,3,4,5,6]:");
                option = sc.nextInt();

                 switch (option){
                    case 1: 
                    case 2: 
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    option = sc.nextInt();
                    break;
                }
        break;

        case 4: System.out.println("Loans Sub-Menu:");
                System.out.println("1. Insert"); 
                System.out.println("2. Search"); 
                System.out.println("3. Delete"); 
                System.out.println("4. Edit");
                System.out.println("5. List All");
                System.out.println("6. Exit"); 
                System.out.println("Enter your option [1,2,3,4,5,6]:");
                option = sc.nextInt();

                 switch (option){
                    case 1: 
                    case 2: 
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    option = sc.nextInt();
                    break;
                }
        break;

        case 5: System.out.println("Statistics Sub-Menu:");
                System.out.println("1. Top 5 Books"); 
                System.out.println("2. Top 5 Members"); 
                System.out.println("3. Emplyee of the Month"); 
                System.out.println("4. List Overdue");
                System.out.println("5. Exit"); 
                System.out.println("Enter your option [1,2,3,4,5]:");
                option = sc.nextInt();

                 switch (option){
                    case 1: 
                    case 2: 
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    option = sc.nextInt();
                    break;
                }
        break;

        case 6: System.out.println("You selected to Exit");
                System.exit(0);
                break;
    }

   }  while (option!=6);

    }
    }*
4

1 回答 1

0

避免使用单个变量来控制两个不同的开关。在这里,您使用选项变量来控制内部和外部开关。When option is 6 then controls breaks from both switches. 使用另一个变量来控制内部循环。我在下面的代码中使用了 subOption 。

在用户想要退出之前,子菜单功能也应该起作用。所以最好在内部开关内部添加do。

    public class Menu {
    public static void main(String args[]) throws IOException {
    Scanner sc = new Scanner(System.in);
    int option, subOption;

    do {
        System.out.println("Main Menu:");
        System.out.println("1. Books");
        System.out.println("2. Members");
        System.out.println("3. Employees");
        System.out.println("4. Loans");
        System.out.println("5. Statistics");
        System.out.println("6. Exit");
        System.out.println("Enter your option [1,2,3,4,5,6]:");
        option = sc.nextInt();

        switch (option) {
        case 1: {

            do {
                System.out.println("Books Sub-Menu:");
                System.out.println("1. Insert");
                System.out.println("2. Search");
                System.out.println("3. Delete");
                System.out.println("4. Edit");
                System.out.println("5. List All");
                System.out.println("6. Exit");
                System.out.println("Enter your option [1,2,3,4,5,6]:");
                subOption = sc.nextInt();

                switch (subOption) {
                case 1: {
                    System.out.println("in Insert");
                    break;
                }
                case 2: {
                    System.out.println("in Search");
                    break;
                }
                case 3: {
                    System.out.println("in Delete");
                    break;
                }
                case 4: {
                    System.out.println("in Edit");
                    break;
                }
                case 5: {
                    System.out.println("in List All");
                    break;
                }
                case 6: {
                    System.out.println("in Exit");
                    break;
                }
                // option = sc.nextInt();
                }
            } while (subOption != 6);
            break;
        }

        case 2:

        {

            do {
                System.out.println("Members Sub-Menu:");
                System.out.println("1. Insert");
                System.out.println("2. Search");
                System.out.println("3. Delete");
                System.out.println("4. Edit");
                System.out.println("5. List All");
                System.out.println("6. Exit");
                System.out.println("Enter your option [1,2,3,4,5,6]:");
                subOption = sc.nextInt();

                switch (subOption) {
                case 1: {
                    System.out.println("in Insert");
                    break;
                }
                case 2: {
                    System.out.println("in Search");
                    break;
                }
                case 3: {
                    System.out.println("in Delete");
                    break;
                }
                case 4: {
                    System.out.println("in Edit");
                    break;
                }
                case 5: {
                    System.out.println("in List All");
                    break;
                }
                case 6: {
                    System.out.println("in Exit");
                    break;
                }
                // option = sc.nextInt();
                }
            } while (subOption != 6);
            break;
        }

        case 3:

        {

            do {
                System.out.println("Employees Sub-Menu:");
                System.out.println("1. Insert");
                System.out.println("2. Search");
                System.out.println("3. Delete");
                System.out.println("4. Edit");
                System.out.println("5. List All");
                System.out.println("6. Exit");
                System.out.println("Enter your option [1,2,3,4,5,6]:");
                subOption = sc.nextInt();

                switch (subOption) {
                case 1: {
                    System.out.println("in Insert");
                    break;
                }
                case 2: {
                    System.out.println("in Search");
                    break;
                }
                case 3: {
                    System.out.println("in Delete");
                    break;
                }
                case 4: {
                    System.out.println("in Edit");
                    break;
                }
                case 5: {
                    System.out.println("in List All");
                    break;
                }
                case 6: {
                    System.out.println("in Exit");
                    break;
                }
                // option = sc.nextInt();
                }
            } while (subOption != 6);
            break;
        }

        case 4:

        {

            do {
                System.out.println("Loans Sub-Menu:");
                System.out.println("1. Insert");
                System.out.println("2. Search");
                System.out.println("3. Delete");
                System.out.println("4. Edit");
                System.out.println("5. List All");
                System.out.println("6. Exit");
                System.out.println("Enter your option [1,2,3,4,5,6]:");
                subOption = sc.nextInt();

                switch (subOption) {
                case 1: {
                    System.out.println("in Insert");
                    break;
                }
                case 2: {
                    System.out.println("in Search");
                    break;
                }
                case 3: {
                    System.out.println("in Delete");
                    break;
                }
                case 4: {
                    System.out.println("in Edit");
                    break;
                }
                case 5: {
                    System.out.println("in List All");
                    break;
                }
                case 6: {
                    System.out.println("in Exit");
                    break;
                }
                // option = sc.nextInt();
                }
            } while (subOption != 6);
            break;

        }

        case 5: {

            do {
                System.out.println("Statistics Sub-Menu:");
                System.out.println("1. Top 5 Books");
                System.out.println("2. Top 5 Members");
                System.out.println("3. Emplyee of the Month");
                System.out.println("4. List Overdue");
                System.out.println("5. Exit");
                System.out.println("Enter your option [1,2,3,4,5]:");
                subOption = sc.nextInt();

                switch (subOption) {
                case 1: {
                    System.out.println("in Top 5 Books");
                    break;
                }
                case 2: {
                    System.out.println("in Top 5 Members");
                    break;
                }
                case 3: {
                    System.out.println("in Emplyee of the Month");
                    break;
                }
                case 4: {
                    System.out.println("in List Overdue");
                    break;
                }

                case 5: {
                    System.out.println("in Exit");
                    break;
                }
                // option = sc.nextInt();
                }
            } while (subOption != 6);
            break;

        }

        case 6:
            System.out.println("You selected to Exit");
            System.exit(0);
            break;
        }

    } while (option != 6);

  }
}
于 2014-01-01T11:01:30.000 回答