如何在Java中创建嵌套菜单?

时间:2016-01-31 17:36:57

标签: java oop switch-statement case-statement

我试图创建一个菜单系统,用户选择是否有管理员或客户,然后他们可以输入登录详细信息。

在管理员中签名后,客户会看到不同的菜单。我也希望能够注销,它将返回登录菜单,询问是否有管理员或客户。

这是我到目前为止所做的:

import java.util.Scanner;

public class Main {
    public static void main(String[] args){

        login login = new login();
        Scanner in = new Scanner(System.in);

        String user;
        String pwd;


        int loginOption = 0;

        login.databaseConnection();

        System.out.println("======================================");
        System.out.println("|     Please select and option:      |");
        System.out.println("======================================");
        System.out.println("| Options:                           |");
        System.out.println("|        [1] Admin                   |");
        System.out.println("|        [2] Account Holder          |");
        System.out.println("|        [3] Exit                    |");
        System.out.println("======================================");



        do{
            System.out.print("Option:");
            loginOption = in.nextInt();

            // Switch construct
            switch (loginOption) {
                case 1:
                    System.out.println("");
                    System.out.println("Username: ");
                        user = in.next();
                    System.out.println("Password: ");
                        pwd = in.next();
                    System.out.println("");
                  break;
                case 2:
                    System.out.println("");
                    System.out.println("Account Number: ");
                        user = in.next();
                    System.out.println("Password: ");
                        pwd = in.next();
                    System.out.println("");

                    if(login.validate_login(user, pwd)){
                         System.out.println("Valid Login");
                         System.out.println("");
                         System.out.println("Hello, Account Holder");
                         System.out.println("");
                         System.out.println("======================================");
                         System.out.println("|     Please select and option:      |");
                         System.out.println("======================================");
                         System.out.println("| Options:                           |");
                         System.out.println("|        [1] Check balence           |");
                         System.out.println("|        [2] Withdraw                |");
                         System.out.println("|        [3] Logout                  |");
                         System.out.println("======================================");

                         int userOption = 0;

                         do{
                             System.out.println("Option");
                             userOption = in.nextInt();
                             switch (userOption){
                                case 1:
                                    break;
                                case 2:
                                    break;
                                case 3:
                                    loginOption = 3;
                                    break;
                             }
                         }while (loginOption != 3);
                    }else{
                         System.out.println("Invalid");
                    }
                  break;
                case 3:
                    System.out.println("Exit selected");
                    System.exit(0);
                    break;
                default:
                    System.out.println("Invalid selection");
                    break; // This break is not really necessary
            }
        }while (loginOption != 3);


        in.close();   
    }
}

我遇到的主要问题是离开第二个开关并返回第一个开关。

任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:1)

您可以像这样添加一个布尔变量ext

                      boolean ext = true;
                      do{
                        ...
                            case 3:
                                loginOption = 3;
                                ext = false;
                                break;
                         }
                     }while (loginOption != 3 && ext);

有了这个,当你有loginOption = 3;

时,你可以退出while循环