小编典典

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

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: ");

如果他们选择选项LJ或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;
    }
}

阅读 297

收藏
2020-11-30

共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;
        }
    }
}
2020-11-30