Java switch语句中的旁路情况

Java switch语句中的旁路情况,java,switch-statement,case,Java,Switch Statement,Case,我正试图阻止switch语句中的其他案例在位于最初满足的案例下方时自动打印。这是我正在建立的代码。我以前读过,在switch语句(?)中,错误是不可避免的,但我认为一定有办法解决这个问题 System.out.println("\nQ1) What is the capital of Alaska?"); System.out.println("\n\t 1) Melbourne"); System.out.println("\n\t 2) Anchorage")

我正试图阻止switch语句中的其他案例在位于最初满足的案例下方时自动打印。这是我正在建立的代码。我以前读过,在switch语句(?)中,错误是不可避免的,但我认为一定有办法解决这个问题

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!");

用“break”结束每个case语句块

例如


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

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

switch (var)  {
    case a:
       ... 
       break;
    case b:
       ....
       break;
    default:
       ...
} 
不仅是C语言,而且所有受Fortran计算转到功能影响的语言都会出现故障。在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一起使用时阻止了它们的执行。您需要单独的切换案例块来检查每个答案。

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

  • 但是,Switch Case在找到匹配的Case时不会停止执行,直到出现break语句。要终止进一步执行其他情况产生的流,在每种情况下都必须使用break语句
  • 如果没有匹配的大小写,并且缺省语句是开关块中的最后一个条件,则执行缺省语句。如果默认语句显示为第一个块,则即使存在匹配的情况,也会自动执行该语句。因此,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“);
    }
    

  • 您的问题是什么?请提供再现问题的最小代码,而不是所有代码。@然后,我正在寻找一种方法,以避免打印不涉及使用break的每个案例结果,因为这会停止所有剩余代码的运行,并阻止用户继续完成剩余的测试。这很有意义,我尝试了break的代码,很明显它是有效的。我特别要问的是,是否有一种方法可以继续运行进行中断的其余语句。这个特殊的程序是用来做测验的,所以不管用户输入的答案是正确的还是错误的,都必须有一种方法来继续处理其余的问题。@Rodanten抱歉,但我无法理解这个问题。你能详细说明一下吗?基本上我在两个结果之间左右为难。在案例之后使用break将阻止其余问题的运行,但也会阻止用户继续处理其余问题。不使用break允许用户继续,但它也会打印每个案例结果,而不仅仅是与他们的选择相对应的结果。我正在寻找一种方法,让用户继续并跳过那些不适用于他们选择的答案的案例。这正是我想要实现的。非常感谢你的帮助。这让我相信我所尝试的可能是不可能的。如果继续执行的案例始终执行到中断,则用户不可能在中断语句之后继续处理剩余的问题,同时也看不到与他们的选择不一致的其他案例的打印。@rodanten,我不确定您试图用哪种语言实现解决方案,不过,你可以用简单的递归函数来实现它。@Lakshay Garg实际上能够帮助我实现我所搜索的输出。最终,解决方案相当简单。同样感谢您的帮助。这是我以前的解决方案,但由于它停止了其余代码的运行,因此不允许用户继续完成其余的测试。
    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“);
    }