我正在尝试使我的 switch case 循环在用户获得默认选项时再次回到开始。

huangapple go评论73阅读模式
英文:

I am trying to make my switch case loop back again if they get the user gets the default

问题

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("欢迎来到选择游戏...");
    System.out.println("请输入您的姓名:");

    String playerName = input.nextLine();

    System.out.println(playerName + "喜欢的宠物是什么?");
    System.out.println("a. 狗\nb. 猫");

    // 选择是狗还是猫
    String pet = input.next();
    do {
        switch (pet.charAt(0)) {
            case 'a' -> {
                System.out.println("您的狗叫什么名字?");
                String dogsName = input.next();
                System.out.println("您的角色名是:" + playerName + "\n您的宠物名是:" + dogsName);
                break;
            }
            case 'b' -> {
                System.out.println("您的猫叫什么名字?");
                String catsName = input.next();
                System.out.println("角色名:" + playerName + "\n宠物名:" + catsName);
                break;
            }
            default -> System.out.println("这不是一个有效的选项。请重新选择。");
        }
    } while (pet.charAt(0) != 'a' && pet.charAt(0) != 'b');

    input.close();
}
英文:

I can't find a loop to use that would bring it back to the beginning of the switch case and repeatedly until the user answers using one of the choices, Any help would be awesome! Thanks. (I have also tried using a do-while loop that someone suggested but it just seems to spam the default.) ((which I left in the code))

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

答案1

得分: 1

你需要在do循环中接受猫/狗的输入(选项a或选项b),这样在错误输入后,代码可以要求更新的输入。如下所示:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("    欢迎来到选择游戏...    ");
    System.out.println("请输入您的姓名:");

    String playerName = input.nextLine();

    System.out.println(playerName + "喜欢什么宠物?");
    System.out.println("a. 狗 \nb. 猫");

    // 这里是代码的更改
    String pet = null;
    do {
        // 选择狗或猫
        // 这里是代码的更改
        pet = input.next();
        switch (pet.charAt(0)) {
            case 'a': {
                System.out.println("你的狗叫什么名字?");
                String dogsName = input.next();
                System.out.println("你的角色名字是:" + playerName + "\n你的宠物名字是:" + dogsName);
                break;
            }
            case 'b': {
                System.out.println("你的猫叫什么名字?");
                String catsName = input.next();
                System.out.println("角色名字:" + playerName + "\n宠物名字:" + catsName);
                break;
            }
            default: {
                System.out.println("这不是一个有效的选项。请重新选择。");
            }
        }

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

    input.close();

}
英文:

You need to take the input for Cat/Dog (option a or option b) inside the do loop so that after wrong input code can ask for updated input. As below:

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

huangapple
  • 本文由 发表于 2020年8月18日 00:03:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63454540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定