绕过switch语句中的case

时间:2015-06-18 01:41:10

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

3 个答案:

答案 0 :(得分:1)

以“break;”

结束每个case语句块

例如

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

答案 1 :(得分:0)

此行为的正确用语是 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 块来检查每个答案。

答案 2 :(得分:0)

切换案例执行如下所述..从下面的例子中,如果用户输入“1”然后执行案例1,或者输入为“2”则执行案例2,它将一直持续到案件数包含在案例中切换块。

  1. 但是,Switch-Case在找到匹配的大小写时才会停止执行,直到出现break语句。要终止进一步执行其他情况的流程,在每种情况下都必须使用break语句。
  2. 如果没有匹配的情况并且默认语句是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“);
    }