开发者社区> 问答> 正文

如何在没有用户输入的情况下自动返回主菜单?

“主菜单”是此处的位置System.out.print("enter letter P T L S A or Q");。用户输入其姓氏后,控制台应自动返回到第一个System.out.print语句,即菜单。我如何将其设置为这样?

class myOwnTryAginLoopThatWorks{
    public static void main (String [] args){
        Scanner console = new Scanner(System.in);
        boolean valid;
        char choice = '\0';
        String persnr;
        do{
            valid = true;
            System.out.print("enter letter P T L S A or Q"); //menu
            String input = console.nextLine();
            if (!isValid(input)){
                valid = false;
                System.out.print("You did not enter the correct menu option, ");
                if (input.length() != 1) {
                    System.out.println("and your input length is too long (must be 1 character long), ");
                    valid = false;
                }
            }
            choice = input.charAt(0);
        }while(!valid);

        switch (choice) {
            case 'P':
                    do {
                        valid = true; 
                        System.out.print("Enter persnr in the format of (YYYYMMDD): ");
                        try {
                            persnr = console.nextLine();
                            if (!persnr.matches("[1-9]{1}[0-9]{7}")) {
                                throw new IllegalArgumentException("You printed wrong format, try again");
                            }
                            System.out.println("Processsing...");
                        } catch (IllegalArgumentException e) {
                        System.out.println(e.getMessage());
                        valid = false;
                        }
                    } while (!valid);
                    break;
        }

        System.out.print("enter first name ");
        String firstName = console.nextLine();
        System.out.print("enter surrname ");
        String surName = console.nextLine();

    } 

    public static boolean isValid (String n){ 
        switch(n){
            case "P":
            case "T":
            case "L":
            case "S":
            case "A":
            case "Q":
                    return true;
            default:
                    return false;
        }
    }    
}

问题来源:Stack Overflow

展开
收起
montos 2020-03-22 14:14:03 681 0
1 条回答
写回答
取消 提交回答
  • 您必须更好地提出这个问题。据我了解,您需要再次显示菜单,要求输入其他数据。

    private static boolean exit = false;
    public static void main(String[] args) {
    
        while (exit == false){
             readValues();
        }
    
    
    }
    
    private static boolean isValid(String n) {
    
        switch (n) {
        case "P":
        case "T":
        case "L":
        case "S":
        case "A":
        case "Q":
            return true;
        default:
            return false;
    
        }
    }
    
    private static void readValues() {
        Scanner console = new Scanner(System.in);
    
        boolean valid;
        char choice = '\0';
        String persnr;
    
        do {
            valid = true;
            System.out.print("enter letter P T L S A or Q"); //menu
            String input = console.nextLine();
    
            if (!isValid(input)) {
                valid = false;
                System.out.print("You did not enter the correct menu option, ");
    
                if (input.length() != 1) {
                    System.out.println("and your input length is too long (must be 1 character long), ");
                    valid = false;
                }
            }
    
            choice = input.charAt(0);
    
        } while (!valid);
    
        switch (choice) {
        case 'P':
    
            do {
                valid = true;
                System.out.print("Enter DATE in the format of (YYYYMMDD): ");
    
                try {
                    persnr = console.nextLine();
    
                    if (!persnr.matches("[1-9]{1}[0-9]{7}")) {
                        throw new IllegalArgumentException("You printed wrong format, try again");
                    }
    
                    System.out.println("Processsing...");
    
                } catch (IllegalArgumentException e) {
                    System.out.println(e.getMessage());
                    valid = false;
                }
    
            } while (!valid);
            break;
    
        }
    
        System.out.print("enter first name ");
        String firstName = console.nextLine();
    
        System.out.print("enter surrname ");
        String surName = console.nextLine();
    
        String query = " insert into Person (PNr, FName, ENamn)"
             + " values (persnr, firstName, surName)";
    
    }
    

    回答来源:Stack Overflow

    2020-03-22 14:14:45
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多