Estoy tratando de hacer que mi caso del interruptor vuelva a repetirse si obtienen el usuario obtiene el valor predeterminado

Aug 18 2020

No puedo encontrar un bucle para usar que lo devuelva al principio del caso del interruptor y repetidamente hasta que el usuario responda usando una de las opciones. ¡Cualquier ayuda sería increíble! Gracias. (También intenté usar un bucle do-while que alguien sugirió, pero parece enviar spam por defecto.) ((Que dejé en el código))

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);


    System.out.println("    Welcome To The Choices Game...    ");
    System.out.println("Please enter your name: ");

    String playerName = input.nextLine();

    System.out.println("What is " + playerName + "'s" + " favorite pet?");
    System.out.println("a. Dog \nb. Cat");

    //Choice of choosing a dog or a cat
    String pet = input.next();
    do {


        switch (pet.charAt(0)) {
            case 'a' -> {
                System.out.println("What is your dog's name? ");
                String dogsName = input.next();
                System.out.println("Your Character's Name is: " + playerName + "\nYour Pet's Name is: " + dogsName);
                break;
            }
            case 'b' -> {
                System.out.println("What is your cat's name? ");
                String catsName = input.next();
                System.out.println("Character Name: " + playerName + "\nPet Name: " + catsName);
                break;
            }
            default -> System.out.println("That is not a valid option. Please choose again.");
        }

    } while (pet.charAt(0) != 'a' && pet.charAt(0) != 'b');

    input.close();

}

Respuestas

1 notescrew Aug 18 2020 at 00:03

Debe tomar la entrada para Gato / Perro (opción ao opción b) dentro del bucle do para que después de un código de entrada incorrecto pueda solicitar una entrada actualizada. Como a continuación:

public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("    Welcome To The Choices Game...    ");
        System.out.println("Please enter your name: ");

        String playerName = input.nextLine();

        System.out.println("What is " + playerName + "'s" + " favorite pet?");
        System.out.println("a. Dog \nb. Cat");

        // Here is change in code
        String pet = null;
        do {
            // Choice of choosing a dog or a cat
            // Here is change in code
            pet = input.next();
            switch (pet.charAt(0)) {
            case 'a': {
                System.out.println("What is your dog's name? ");
                String dogsName = input.next();
                System.out.println("Your Character's Name is: " + playerName + "\nYour Pet's Name is: " + dogsName);
                break;
            }
            case 'b': {
                System.out.println("What is your cat's name? ");
                String catsName = input.next();
                System.out.println("Character Name: " + playerName + "\nPet Name: " + catsName);
                break;
            }
            default: {
                System.out.println("That is not a valid option. Please choose again.");
            }
            }

        } while (pet.charAt(0) != 'a' && pet.charAt(0) != 'b');

        input.close();

    }