【问题标题】:[BEGINNER]set up a loop in switch default[BEGINNER]在switch default中设置循环
【发布时间】:2025-11-30 20:35:01
【问题描述】:

作为我的第一个小项目,我正在尝试设置一个测验。我编写了生成问题和答案的方法。我现在正试图强制用户输入 1 或 2 以继续并循环,而他没有。 在这一点上我有点迷路了。我是否遗漏了一些非常明显的东西?

public static boolean generateQuestion2(String question, String answer1, String answer2) {
    boolean bingo;
    System.out.println(question);
    System.out.println("(1) " + answer1);
    System.out.println("(2) " + answer2); //bingo

    Scanner scan1 = new Scanner(System.in);
    int antwort = scan1.nextInt();

    switch (antwort) {
        case 1:
            System.out.println("Falsch...");
            return bingo = false;

        case 2:
            System.out.println("Richtig!");
            return bingo = true;

        default:
            // Here I intend to loop while antwort != 1 and 2
            while (antwort != 1 && antwort != 2) {
                System.out.println(" Bitte 1 doer 2 eintippen");
                return bingo = false;
            }
            return bingo = false;
    }
}

【问题讨论】:

  • 你写的时间无关紧要。 while 应该包含 nextInt() 和整个 switch 块

标签: java loops switch-statement


【解决方案1】:

循环中没有修改antwort的内容,需要从scan1重新读取。

此外,您在函数内部无条件地从函数返回。这将立即返回。

并且循环可能应该围绕整个switch

最后,对于只有两个值,我宁愿使用简单的if ... else if

【讨论】:

  • 是的,考虑过使用 if ... else,但我可能希望最多有四个答案选项,一个 switch 似乎有更大的增长潜力。
  • @Buddy 然后switch 更有意义。
【解决方案2】:

按如下方式进行:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // Test
        System.out.println(generateQuestion2("x", "y", "z"));
    }

    public static boolean generateQuestion2(String question, String answer1, String answer2) {
        boolean bingo = false;
        boolean valid = true;
        System.out.println(question);
        System.out.println("(1) " + answer1);
        System.out.println("(2) " + answer2); // bingo

        Scanner scan1 = new Scanner(System.in);
        int antwort = scan1.nextInt();
        do {
            valid = true;
            switch (antwort) {
            case 1:
                System.out.println("Falsch...");
                bingo = false;
                break;
            case 2:
                System.out.println("Richtig!");
                bingo = true;
                break;
            default:
                valid = false;
                System.out.println(" Bitte 1 doer 2 eintippen");
                antwort = scan1.nextInt();
            }
        } while (!valid);
        return bingo;
    }
}

示例运行:

x
(1) y
(2) z
4
 Bitte 1 doer 2 eintippen
3
 Bitte 1 doer 2 eintippen
2
Richtig!
true

另一个示例运行:

x
(1) y
(2) z
4
 Bitte 1 doer 2 eintippen
3
 Bitte 1 doer 2 eintippen
1
Falsch...
false

另一个示例运行:

x
(1) y
(2) z
1
Falsch...
false

另一个示例运行:

x
(1) y
(2) z
2
Richtig!
true

【讨论】:

    【解决方案3】:

    您在循环中返回,因此您将退出该方法并且循环将停止。 你想要在那个循环中等待用户输入int antwort = scan1.nextInt();。收到输入后,您想回到您的交换机。

    这样的……

    // here we wait for input
    int antwort = scan1.nextInt();
    while (true) {
      if (antwort == 1 || antwordt == 2) {
        return processAntwort(antwort);
      } else {
        System.out.println(" Bitte 1 doer 2 eintippen");
        // here again we wait for input
        antwort = scan1.nextInt();
      }
    }
    private boolean processAntwort(int antwort) {
      return antwort == 2;
    }
    

    【讨论】: