Sto cercando di fare di nuovo il loop del mio caso switch se ottengono che l'utente ottiene l'impostazione predefinita

Aug 18 2020

Non riesco a trovare un ciclo da utilizzare che lo riporti all'inizio del caso dello switch e ripetutamente fino a quando l'utente non risponde utilizzando una delle scelte, Qualsiasi aiuto sarebbe fantastico! Grazie. (Ho anche provato a utilizzare un ciclo do-while suggerito da qualcuno, ma sembra che invii spam all'impostazione predefinita.) ((Che ho lasciato nel codice))

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

}

Risposte

1 notescrew Aug 18 2020 at 00:03

È necessario prendere l'input per Cat / Dog (opzione a o opzione b) all'interno del ciclo do in modo che dopo un codice di input errato possa chiedere l'input aggiornato. Come sotto:

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

    }