Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果他们得到默认值,我试图使我的开关盒再次循环_Java_Loops - Fatal编程技术网

Java 如果他们得到默认值,我试图使我的开关盒再次循环

Java 如果他们得到默认值,我试图使我的开关盒再次循环,java,loops,Java,Loops,我找不到一个循环可以让它回到案例a并重复使用,直到用户使用其中一个选项回答,任何帮助都会很棒!感谢要循环整个switch case部分,您可以尝试将其放入do while循环中。关于do while块使用的条件,您可以尝试:- do{…}while(pet.charAt(0)!='a'| | pet.charAt(0)!='b') 我为您提供以下可能的解决方案:- //Choice of choosing a dog or a cat String pet = input.n

我找不到一个循环可以让它回到案例a并重复使用,直到用户使用其中一个选项回答,任何帮助都会很棒!感谢

要循环整个switch case部分,您可以尝试将其放入do while循环中。关于do while块使用的条件,您可以尝试:-
do{…}while(pet.charAt(0)!='a'| | pet.charAt(0)!='b')

我为您提供以下可能的解决方案:-

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

}

}

使用
do while
循环是解决此问题的简单方法

我使用变量
repeat
检查是否需要再次请求输入

另外,请注意,现在即使我添加了另一个case(比如
case'c'
),我也不需要修改
do while
循环的条件

布尔重复;
做{
字符串pet=input.next();
重复=错误;
开关(宠物特征(0)){
案例‘a’->{
System.out.println(“你的狗叫什么名字?”);
字符串dogsName=input.next();
System.out.println(“您的角色名为:“+playerName+”\n您的宠物名为:“+dogsName”);
}
案例“b”->{
System.out.println(“你的猫叫什么名字?”);
字符串catsName=input.next();
System.out.println(“字符名:+playerName+”\n设置名:+catsName);
}
默认值:System.out.println(“这不是一个有效选项,请重新选择”);
重复=正确;
}
}同时(重复);
input.close();

循环是一个很好的解决方案,但您也可以使用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

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

我会设置一个布尔标志“IsErrorAnswer”,它在默认情况下设置为true,在正确情况下设置为false。这样你就不会重复检查“a”或“b”。是的,这是另一个可行的解决方案。您可以检查布尔标志的值,而不是检查输入的字符。谢谢,但当我使用此解决方案时,它会一次又一次地重复发送默认值。我已经编辑了代码。do while块的while条件中存在逻辑错误。这应该运行良好。再次感谢!!但出于某种原因,它一直存在相同的问题:/此解决方案也会垃圾邮件默认值,但谢谢。
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();
        }
       
}