英文:
How to automatically return to main menu without user input?
问题
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("请输入字母 P T L S A 或 Q:"); // 菜单
            String input = console.nextLine();
            if (!isValid(input)) {
                valid = false;
                System.out.print("您未输入正确的菜单选项,");
                if (input.length() != 1) {
                    System.out.println("并且您的输入过长(必须为 1 个字符),");
                    valid = false;
                }
            }
            choice = input.charAt(0);
        } while (!valid);
        switch (choice) {
            case 'P':
                do {
                    valid = true;
                    System.out.print("以 (YYYYMMDD) 格式输入 persnr:");
                    try {
                        persnr = console.nextLine();
                        if (!persnr.matches("[1-9]{1}[0-9]{7}")) {
                            throw new IllegalArgumentException("您输入的格式有误,请重试");
                        }
                        System.out.println("处理中...");
                    } catch (IllegalArgumentException e) {
                        System.out.println(e.getMessage());
                        valid = false;
                    }
                } while (!valid);
                break;
        }
        System.out.print("请输入名字:");
        String firstName = console.nextLine();
        System.out.print("请输入姓氏:");
        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;
        }
    }
}
英文:
The "main menu" is the place where System.out.print("enter letter P T L S A or Q");. When the user has entered his/her surname, the console should automatically return to the first System.out.print statement i.e the menu. How do I set it up to be like that?
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;
}
}    
}
答案1
得分: 2
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)";
}
英文:
> You MUST ask the question better. As I understand it, you need your menu is shown again asking to enter another data.
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)";
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论