英文:
I am trying to make my switch case loop back again if they get the default
问题
//选择狗还是猫
String pet = input.next();
switch (pet.charAt(0)) {
case 'a' -> {
System.out.println("你的狗叫什么名字?");
String dogsName = input.next();
System.out.println("你的角色名字是:" + playerName + "\n你的宠物名字是:" + dogsName);
}
case 'b' -> {
System.out.println("你的猫叫什么名字?");
String catsName = input.next();
System.out.println("角色名字:" + playerName + "\n宠物名字:" + catsName);
}
default -> System.out.println("这不是一个有效的选项。请重新选择。");
}
input.close();
英文:
//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();
}
I can't find a loop to use that would bring it back to case a and repeatedly until the user answers using one of the choices, Any help would be awesome! Thanks
答案1
得分: 2
使用do while
循环是解决这个问题的一种简单方法。
我使用了变量repeat
来检查是否需要重新要求输入。
另外,需要注意的是,即使我添加另一个情况(比如说case 'c'
),我也不需要修改do while
循环的条件。
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();
英文:
Using do while
loop is a simple way to solve it.
I used a variable repeat
to check, If I need to ask for the input again or not
Also, note that now even If I add another case(say case 'c'
) I don't need to modify condition for my do while
loop
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();
答案2
得分: 0
要循环整个switch-case部分,您可以尝试将其放在一个do-while循环中。至于与do-while块一起使用的条件,您可以尝试使用:
do {
// ...
} while (pet.charAt(0) != 'a' || pet.charAt(0) != '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. 猫");
// 选择是狗还是猫
do {
String 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();
}
英文:
To loop the entire switch-case section, you can try putting it in a do-while loop. As for the condition to use with the do-while block you could try:-
do
{...}while(pet.charAt(0)!='a' || pet.charAt(0)!='b');
I am providing you with a possible solution 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");
//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();
}
答案3
得分: 0
循环是一个好的解决方案,但你也可以在Java中使用递归方法。
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. 猫");
method(playerName, input);
input.close();
}
public static void method(String playerName, Scanner input) {
String pet = input.next();
switch (pet.charAt(0)) {
case 'a' -> {
System.out.println("你的狗叫什么名字?");
String dogsName = input.next();
System.out.println("你的角色名字是:" + playerName + "\n你的宠物名字是:" + dogsName);
}
case 'b' -> {
System.out.println("你的猫叫什么名字?");
String catsName = input.next();
System.out.println("角色名字:" + playerName + "\n宠物名字:" + catsName);
}
default -> {
System.out.println("这不是一个有效的选项,请重新选择。");
method(playerName, input);
}
}
}
请注意,我在代码中进行了一些调整以使其更加符合Java的语法规范,并且使用了静态方法。
英文:
Looping is good solution but you can also use the Recessive methods 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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论