【问题标题】:I am trying to make my switch case loop back again if they get the default如果他们获得默认值,我正在尝试让我的开关盒再次循环
【发布时间】:2020-08-17 04:56:39
【问题描述】:
    //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();

}

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

【问题讨论】:

    标签: java loops


    【解决方案1】:

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

    【讨论】:

    • 这个解决方案也默认垃圾邮件,但是谢谢。
    【解决方案2】:

    循环是很好的解决方案,但您也可以在 java 中使用 Recessive 方法。

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

    【讨论】:

      【解决方案3】:

      要循环整个 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("    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();
      

      }

      【讨论】:

      • 我会设置一个布尔标志“isWrongAnswer”,在默认情况下设置为 true,在正确情况下设置为 false。这样您就不会重复检查“a”或“b”。
      • 是的,这是另一个可行的解决方案。您可以检查布尔标志的值,而不是检查输入的字符。
      • 谢谢,但是当我使用这个解决方案时,它会一遍又一遍地发送垃圾邮件。
      • 我已经编辑了代码。 do-while 块的 while 条件存在逻辑错误。这应该可以正常运行。
      • 再次感谢!!但由于某种原因,它一直有同样的问题:/
      猜你喜欢
      • 2020-12-06
      • 1970-01-01
      • 2023-03-14
      • 2018-07-24
      • 2011-12-11
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 2019-10-16
      相关资源
      最近更新 更多