用户选择时使用开关盒和while循环进行无限循环

时间:2017-02-20 14:57:55

标签: java

我正在尝试使用用户输入创建一个CLI,以便从可用选项中进行选择。我正在使用switchwhile循环来确保在选择选项后它可以返回主菜单。问题是,在用户成功登录后,它将转到案例4,但它会重复(无限)案例4,我不确定原因。

以下是代码:

public class MainApplication {

    private static User userLoggedIn;
    private static Device deviceLoggedIn;
    private static PrivateKey privateKey;
    private static AbePrivateKey secretKey;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
//      CpabeDAO.em.getTransaction().begin();

        System.out.println("Welcome to CPABE Cloud");

        int test = 0;
        while (true){
            switch(test){
            case 0: //Greeting for the user. User now chooses the option.
                System.out.println("Please choose the option by typing the option number:");
                System.out.println("(1) Login");
                System.out.println("(2) Register User");
                System.out.println("(3) Exit");
                int input = scanner.nextInt();
                if(input < 0 ||  input > 3){
                    System.out.println("Unrecognized input.");
                    test = 0;
                }
                else {
                    test = input;
                }
                break;
            case 1:
                System.out.println("Login");
                List<User> userRetrieve = login();
                //check if it is successfully logged in
                boolean loggedIn = (userRetrieve.size() == 0);
                if(!loggedIn) {
                    for(User user : userRetrieve) {
                        userLoggedIn = user;
                    }
                    //Retrieve private key & secret key from cpabe dir, if it is not stored then register new device. After that go to case 4, which is the main menu;
                    test = 4;
                }
                else{ 
                    System.out.println("Your username or password is incorrect. Please choose what to do next:");
                    System.out.println("(1) Login");
                    System.out.println("(2) Back to main menu");
                    int input2 = scanner.nextInt();
                    if(input2 == 1){
                        test = 1;
                    }
                    else if(input2 == 2){
                        test = 0;
                    }
                }
                break;
            case 2:
                System.out.println("Register User");                        

                userLoggedIn = UserRegistration.registerUser();
                 DeviceRegistration.registerNewDeviceforNewUser(userLoggedIn);

                test = scanner.nextInt();   
                break;
            case 3:
                System.out.println("Exit");
                System.out.println("Press 0 to go back to menu");
                test = scanner.nextInt();           
                break;
            case 4: //the main menu. user can have full functionalities after logging in or registering.
                System.out.println("Welcome " + userLoggedIn.getFirstname());
                System.out.println("Please select the option by typing the option number:");
                break;
             default :
                System.out.println("Invalid input. Please insert the correct input.");
                test = 0;
            }
        }
    }

    private static List<User> login(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter your username:");
        String username = scanner.nextLine();
        System.out.println("Please enter your password:");
        String password = scanner.nextLine();       
        String hashPassword = Encryption.generateHash(password.getBytes());
        Query q = CpabeDAO.em.createQuery("SELECT u FROM User u WHERE u.username = '" + username + "' and u.hashPassword = '" + hashPassword + "'");
        List<User> userRetrieve = q.getResultList();
        scanner.close();
        return userRetrieve;
    }

我想要实现的是,在用户成功登录后,系统将转到案例4,即主菜单,并等待用户的输入,但不断重复案例4。

1 个答案:

答案 0 :(得分:1)

正如@Abubakkar所说,你不能从开关中断你的循环。另外,您继续保留第四种情况,因为您永远不会更改test值,您永远不会要求扫描仪提供其他值,因此它会保留在4。如果要返回菜单,应该直接从交换机将其放回0。 你可以这样做:

    boolean firstMenu = true;
    boolean secondmenu = false;
    while(firstMenu){
        [...]
        case 4: //the main menu. user can have full functionalities after logging in or registering.
            System.out.println("Welcome " + userLoggedIn.getFirstname());
            System.out.println("Please select the option by typing the option number:");
            firstMenu = false;
            secondMenu = true;
            break;
        }
        [...]
    }
    while(secondMenu){
        [Your second menu]
    }

请参阅This,它可以帮助您从循环中退出,第一个和第二个答案是解决问题的绝佳选择:)