在选择选项 - Java之后,如何向用户显示我再次创建的菜单

时间:2015-01-10 21:32:56

标签: java

我是初学者,我想向用户显示以下内容

System.out.println("Welcome to the client screen");
System.out.println("There are 3 Lotteries (Lotto, Jackpot and National)");
System.out.println("Please select one (L, J, N) or E to exit: "); 

如果菜单选择L J或N,则再次进行菜单,因此用户必须再次选择另一个字母或选择退出。我还不确定我是否实施了案例' E'正确,但我认为这是我可以再次向用户显示以前的登录屏幕,所以如果有人可以确认对我来说这将是伟大的!

System.out.println("--------Login Screen--------");

System.out.println("Enter C for client and E for employee: ");

String login= s.nextLine();

if("C".equals(login)) {
    System.out.println("Welcome to the client screen");
    System.out.println("There are 3 Lotteries (Lotto, Jackpot and National)");
    System.out.println("Please select one (L, J, N) or E to exit: ");
    String select= s.nextLine();

    switch(select){    
        case "L": 
                 break;
        case "J": 
                 break;
        case "N": 
                 break;   
        case "E": System.out.println("--------Login Screen--------");
                  System.out.println("Enter C for client and E for employee: ");
                  login= s.nextLine();  
                 break;
        default: System.out.println("Invalid Selection");
                 break;
    }
}

2 个答案:

答案 0 :(得分:1)

为了让客户端菜单在完成时返回登录菜单,添加两个while循环,一个用于登录屏幕,另一个用于客户端屏幕,如下所示:

boolean loginScreenDone=false;

while(!loginScreenDone) {
    System.out.println("--------Login Screen--------");

    System.out.println("Enter C for client and E for employee: ");

    String login = s.nextLine();


    if ("C".equals(login)) {
        boolean clientScreenDone=false;

        while(!clientScreenDone) {
            System.out.println("Welcome to the client screen");
            System.out
                .println("There are 3 Lotteries (Lotto, Jackpot and National)");
            System.out.println("Please select one (L, J, N) or E to exit: ");
            String select = s.nextLine();

            switch (select) {
            case "L":
                //call a method to handle Lotto here
                break;
            case "J":
                //call a method to handle Jackpot here
                break;
            case "N":
                //call a method to handle National here
                break;
            case "E":
                clientScreenDone=true;
                break;
            default:
                System.out.println("Invalid Selection");
                break;
        }
    }
}

答案 1 :(得分:0)

创建一个显示这些消息的方法,只要您想显示它们就调用它 关于案例EI认为它是正确的(如果我理解你的做法是正确的)但是因为如果你想要输入E和C,你需要接受输入(s.readLine())。