Sto provando a fare di nuovo il loop della mia custodia se ottengono l'impostazione predefinita

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

}

Non riesco a trovare un loop da utilizzare che lo riporti al caso a e ripetutamente fino a quando l'utente non risponde utilizzando una delle scelte, Qualsiasi aiuto sarebbe fantastico! Grazie

Risposte

1 LoveshDongre Aug 17 2020 at 05:21

Usare il do whileloop è un modo semplice per risolverlo.

Ho usato una variabile repeatper controllare, se ho bisogno di chiedere nuovamente l'input o meno

Inoltre, nota che ora anche se aggiungo un altro caso (diciamo case 'c') non ho bisogno di modificare la condizione per il mio do whileciclo

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

Il looping è una buona soluzione, ma puoi anche usare i metodi Recessive in 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

Per eseguire il loop dell'intera sezione switch-case, puoi provare a metterlo in un loop do-while. Per quanto riguarda la condizione da utilizzare con il blocco do-while potresti provare: -do {...}while(pet.charAt(0)!='a' || pet.charAt(0)!='b');

Ti sto fornendo una possibile soluzione di seguito: -

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

}