Cómo volverá automáticamente al menú principal sin intervención del usuario?

Zerenity:

El "menú principal" es el lugar donde System.out.print("enter letter P T L S A or Q");. Cuando el usuario ha introducido su / su apellido, la consola debe volver automáticamente a la primera declaración System.out.print es decir, el menú. ¿Cómo puedo configurarlo para que sea así?

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;
        }
    }    
}
Adonis González :

Debe solicitar a la mejor pregunta. Como yo lo entiendo, es necesario que su menú se muestra de nuevo que solicita acceder a otros datos.

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

}

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=291864&siteId=1
Recomendado
Clasificación