java - 如何在选择选项后向用户显示我再次创建的菜单 - 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: "); 

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

最佳答案

为了让客户端菜单在完成后返回到登录菜单,添加两个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;
        }
    }
}

关于java - 如何在选择选项后向用户显示我再次创建的菜单 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27881431/

相关文章:

java - Guava的LocalCache无法使用,为什么?

java - 如果在调用链上抛出异常,它的所有子类也会抛出异常吗?

java - Maven SonarQube 多模块

java - 多个一对多关系 ResultSetExtractor

java - Android 如何获取艺术家艺术作品(照片)

java - Java中等效的指针数组

java - 有没有被 if - PMD Violation 包围的日志 block

java - Spring MVC 使用哪个版本的 servlet api?

java - 如何从上面的类中调用方法?

java - 在 Java 中绘制静态图像的最快方法