在 switch 语句中绕过案例

Bypass cases within a switch statement

我试图阻止我的 switch 语句中的其他案例在位于最初满意的案例下方时自动打印。这是我正在构建的代码。我之前读到在 switch 语句中 fallthrough 是不可避免的(?),但我假设必须有办法解决这个问题。

System.out.println("\nQ1) What is the capital of Alaska?");
        System.out.println("\n\t 1) Melbourne");
        System.out.println("\n\t 2) Anchorage");
        System.out.println("\n\t 3) Juneau\n");
        selection = keyboard.nextInt();

        switch (selection){

        default:
        System.out.println("Invalid selection!");
        break;

        case 3:
            System.out.println("\nThat's correct!");

        case 1:
        case 2:
            System.out.println("\nSorry, that's incorrect.");


            if (selection == 3)
            { score++; }

        System.out.print("\nQ2) Can you store the value 'cat' in a variable of type int? ");
        answer = keyboard.next();

        switch (answer){

        case "No":
        case "no":
            System.out.println("\nThat's correct!");

        case "Yes":
        case "yes":
            System.out.println("\nSorry, 'cat' is a string. Int type variables can only store numbers."); 


        if (answer == "No" || answer == "no" )
        { score++; }

            System.out.print("\nQ3) What is the result of 9+6/3?");
            System.out.println("\n\t 1) 5");
            System.out.println("\n\t 2) 11");
            System.out.println("\n\t 3) 15/3\n");
            selection = keyboard.nextInt();

        switch (selection){

        default:
        System.out.println("Invalid selection!");
        break;      

        case 2:
        System.out.println("\nThat's correct!\n");

        case 1:
        case 3:
            System.out.println("\nSorry, that's incorrect.\n");

        if (selection == 2)
        { score++; }

        keyboard.close();

        System.out.print("Overall, you got " + score + " out of 3 correct.\n"
                + "Thanks for playng!");

每个 case 语句块以 "break;"

结束

例如

case 2:
    System.out.println("\nThat's correct!\n");
    break;

此行为的正确术语是 fallthrough。 您需要以 break 语句结束每个案例以获得所需的行为。您可以将 case 关键字视为 goto 的某种形式。

C 和类似语言中 switch-case 语句的典型结构是

switch (var)  {
    case a:
       ... 
       break;
    case b:
       ....
       break;
    default:
       ...
} 

不仅是 C,所有受 Fortran 计算 GOTO 功能影响的语言都失败了。在 Pascal 等语言中不需要 break 语句。

来源:维基百科

编辑:

我已经更正了你的程序:

System.out.println("\nQ1) What is the capital of Alaska?");
System.out.println("\n\t 1) Melbourne");
System.out.println("\n\t 2) Anchorage");
System.out.println("\n\t 3) Juneau\n");
selection = keyboard.nextInt();

switch (selection) {
    case 3:
        System.out.println("\nThat's correct!");
        score++;
        break;
    case 1:
    case 2:
        System.out.println("\nSorry, that's incorrect.");
        break;
    default:
        System.out.println("Invalid selection!");
        break;
}

System.out.print("\nQ2) Can you store the value 'cat' in a variable of type int? ");
answer = keyboard.next();

switch (answer) {
    case "No":
    case "no":
        System.out.println("\nThat's correct!");
        score++;
        break;
    case "Yes":
    case "yes":
        System.out.println("\nSorry, 'cat' is a string. Int type variables can only store numbers."); 
        break;
}

System.out.print("\nQ3) What is the result of 9+6/3?");
System.out.println("\n\t 1) 5");
System.out.println("\n\t 2) 11");
System.out.println("\n\t 3) 15/3\n");
selection = keyboard.nextInt();

switch (selection) {
    case 2:
        System.out.println("\nThat's correct!\n");
        break;
    default:
        System.out.println("Invalid selection!");
        break;      
}

现在很容易看出错误。您没有关闭 switch-case 块,这使得以下语句成为块的一部分,因此在与 break 一起使用时阻止了它们的执行。您需要单独的 switch-case 块来检查每个答案。

开关案例执行如下所述。从下面的示例中,如果用户输入“1”,则执行案例 1,或者输入为“2”,则执行案例 2,它会继续执行,直到包含案例数在开关块中。

  1. 但是,Switch-Case在找到匹配的case时不会停止执行,直到出现break语句。为了终止其他案例的进一步执行流程,在每种情况下都必须使用 break 语句。
  2. 如果没有匹配的case,则执行默认语句,默认语句是switch块中的最后一个条件。如果默认语句出现在第一个块中,那么即使有匹配的情况,它也会自动执行。所以 default 语句应该是 switch-case 语句中的最后一个块。

    switch (input) {
        case 1:  
           System.out.println (“User input is “+ input)
        break;
    
        case 2:  
           System.out.println (“User input is “+ input)
        break;
    
        case 3:  
           System.out.println (“User input is “+ input)
         break;
    
        - - - - 
        - - - -
        - - - -
        default:
           System.out.println(” Invalid input“);
    }