【问题标题】:I am trying to make my switch case loop back again if they get the user gets the default如果他们让用户获得默认值,我正在尝试让我的开关盒再次循环
【发布时间】:2020-12-06 19:22:42
【问题描述】:

我找不到可以使用的循环将其带回到 switch 案例的开头并重复,直到用户使用其中一个选项回答,任何帮助都会很棒!谢谢。 (我也尝试过使用有人建议的 do-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
    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();

}

【问题讨论】:

  • 好的,那么当你尝试你拥有的代码时出了什么问题?
  • 每当我输入除 "a" 或 "b" 之外的另一个 char 或 int 时,它都会进入堆栈溢出,打印出“这不是一个有效的选项。请重新选择。”
  • 而您期望会发生什么?我假设,再次提示用户输入。现在,仔细查看代码。您的代码中应该向用户询问"a""b" 输入的部分是什么?这种情况会发生多少次? 应该发生多少次?是在循环内,还是在循环外? 应该在循环内还是循环外?
  • 阅读ericlippert.com/2014/03/05/how-to-debug-small-programs 了解调试代码的技巧。

标签: java loops switch-statement


【解决方案1】:

您需要在 do 循环中获取 Cat/Dog(选项 a 或选项 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");

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

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2014-12-28
    • 1970-01-01
    • 2021-12-02
    • 2015-02-27
    • 2023-03-17
    • 2022-01-25
    相关资源
    最近更新 更多