J'essaye de rétablir la boucle de mon boîtier de commutateur s'ils obtiennent la valeur par défaut

Aug 16 2020
    //Choice of choosing a dog or a cat

     String 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);
        }
        case 'b' -> {
            System.out.println("What is your cat's name? ");
            String catsName = input.next();
            System.out.println("Character Name: " + playerName + "\nPet Name: " + catsName);
        }
        default -> System.out.println("That is not a valid option. Please choose again.");
    }

   input.close();

}

Je ne trouve pas de boucle à utiliser qui le ramènerait au cas a et à plusieurs reprises jusqu'à ce que l'utilisateur réponde en utilisant l'un des choix, toute aide serait géniale! Merci

Réponses

1 LoveshDongre Aug 17 2020 at 05:21

L'utilisation de la do whileboucle est un moyen simple de le résoudre.

J'ai utilisé une variable repeatpour vérifier, si j'ai besoin de demander à nouveau l'entrée ou non

Notez également que maintenant même si j'ajoute un autre cas (disons case 'c'), je n'ai pas besoin de modifier la condition de ma do whileboucle

boolean repeat;
do {
    String pet = input.next();
    repeat = false;
    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);
        }
        case 'b'-> {
            System.out.println("What is your cat's name? ");
            String catsName = input.next();
            System.out.println("Character Name: " + playerName + "\nPet Name: " + catsName);
        }
        default: System.out.println("That is not a valid option. Please choose again.");
        repeat = true;
    }
} while(repeat);
input.close();
AnveshK Aug 17 2020 at 05:24

Le bouclage est une bonne solution mais vous pouvez également utiliser les méthodes récessives en java.

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
    
        method();
   
    
      input.close();
    
 }

public void method()
{

         String 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);
            }
            case 'b' -> {
                System.out.println("What is your cat's name? ");
                String catsName = input.next();
                System.out.println("Character Name: " + playerName + "\nPet Name: " + catsName);
            }
            default -> System.out.println("That is not a valid option. Please choose again.");
            method();
        }
       
}
Srinjoy_Chatterjee37 Aug 17 2020 at 05:11

Pour boucler toute la section du boîtier de commutation, vous pouvez essayer de la mettre dans une boucle do-while. Quant à la condition à utiliser avec le bloc do-while, vous pouvez essayer: -do {...}while(pet.charAt(0)!='a' || pet.charAt(0)!='b');

Je vous propose une solution possible ci-dessous: -

   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

    do{
     String 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();

}