Próbuję ponownie zapętlić obudowę przełącznika, jeśli otrzymają wartość domyślną

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

}

Nie mogę znaleźć pętli do użycia, która przywróciłaby ją z powrotem do przypadku a i wielokrotnie, dopóki użytkownik nie odpowie, używając jednej z opcji, Każda pomoc byłaby niesamowita! Dzięki

Odpowiedzi

1 LoveshDongre Aug 17 2020 at 05:21

Używanie do whilepętli to prosty sposób na rozwiązanie tego problemu.

Użyłem zmiennej, repeataby sprawdzić, czy muszę ponownie poprosić o dane wejściowe, czy nie

Zauważ też, że teraz nawet jeśli dodam inny przypadek (powiedzmy case 'c'), nie muszę modyfikować warunku dla mojej do whilepętli

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

Pętle to dobre rozwiązanie, ale możesz również użyć metod Recesywnych w Javie.

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

Aby zapętlić całą sekcję przełącznika, możesz spróbować umieścić ją w pętli do while. Jeśli chodzi o warunek do użycia z blokiem do-while, możesz spróbować: -do {...}while(pet.charAt(0)!='a' || pet.charAt(0)!='b');

Poniżej przedstawiam możliwe rozwiązanie: -

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

}