为什么我不能在 Java 的 switch 语句中使用“继续”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2521721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:47:12  来源:igfitidea点击:

Why can't I use 'continue' inside a switch statement in Java?

javaswitch-statementcontinue

提问by abson

Why is it that the following code:

为什么是下面的代码:

class swi  
{
    public static void main(String[] args)  
    {  
        int a=98;
        switch(a)
        {
            default:{ System.out.println("default");continue;}
            case 'b':{ System.out.println(a); continue;}
            case 'a':{ System.out.println(a);}
        }
        System.out.println("Switch Completed");
    }
}

Gives the error:

给出错误:

continue outside of loop

在循环外继续

采纳答案by Michael Aaron Safyan

Falling through is the standard behavior for a switch statement and so, consequently, using continue in a switch statement does not make sense. The continue statement is only used in for/while/do..while loops.

失败是 switch 语句的标准行为,因此,在 switch 语句中使用 continue 没有意义。continue 语句仅用于 for/while/do..while 循环。

Based on my understanding of your intentions, you probably want to write:

根据我对你意图的理解,你可能想写:

System.out.println("default");
if ( (a == 'a') || (a == 'b') ){
    System.out.println(a);
}

I would also suggest that you place the default condition at the very end.

我还建议您将默认条件放在最后。

EDIT: It is not entirely true that continue statements cannot be used inside switch statements. A (ideally labeled) continuestatement is entirely valid. For example:

编辑:不能在 switch 语句中使用 continue 语句并不完全正确。(理想标记的)continue语句是完全有效的。例如:

public class Main {
public static void main(String[] args) {
    loop:
    for (int i=0; i<10; i++) {
        switch (i) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 9:
            continue loop;
        }

        System.out.println(i);
    }
}
}

This will produce the following output: 0 2 4 6 8

这将产生以下输出:0 2 4 6 8

回答by raj

continueinside switch??!!! only breakcan be kept inside switch.!

continue里面的开关??!!!只能break放在switch里面。!

ur code should be,

你的代码应该是,

class swi22  
{
     public static void main(String[] args)  
     {  
         int a=98;
         switch(a)
         {
             default:{ System.out.println("default");break;}
             case 'b':{ System.out.println(a); break;}
             case 'a':{ System.out.println(a);}
          }
          System.out.println("Switch Completed");
     }
}

回答by Chris Farmer

Shouldn't you use breakinstead of continue?

你不应该使用break代替continue吗?

回答by Scott Smith

You are using continuewhere you should be using break

您正在使用continue您应该使用的地方break

回答by T.J. Crowder

Because you have a continueoutside of a loop. continueis for jumping back to the beginning of a loop, but you don't have any loop in that code. What you want for breaking out of a switchcase block is the keyword break(see below).

因为你有continue一个循环之外。continue用于跳回到循环的开头,但该代码中没有任何循环。你想要打破switchcase 块的是关键字break(见下文)。

There's also no need to put every case block within braces (unless you want locally-scoped variables within them).

也不需要将每个 case 块放在大括号中(除非您希望在其中包含局部范围的变量)。

So somethinga bit like this would be more standard:

所以有点像这样的东西会更标准:

class swi22
{
    public static void main(String[] args)
    {
        int a=98;
        switch(a)
        {
            default:
                System.out.println("default");
                break;
            case 'b':
                System.out.println(a);
                break;
            case 'a':
                System.out.println(a);
                break;
        }
        System.out.println("Switch Completed");
    }
}

There's also a school of thought that says the defaultcondition should always be at the end. This is nota requirement, just a fairly widely-used convention.

还有一种思想流派说default条件应该总是在最后。这不是一个要求,只是一个相当广泛使用的约定。

回答by stefanglase

The continue-Statement may be used in loops and not in switch. What you probably want is a break.

continue语句来可以在循环中,而不是在交换机中使用。您可能想要的是一个break.

回答by Kyle Rozendo

continuesimply moves directly to the next iteration of the loop.

continue简单地直接移动到循环的下一次迭代。

breakis used to break out of loops and switches.

break用于跳出循环和开关。

Use break;instead of continue;

使用break;代替continue;

Continue:

继续:

for(x = 0; x < 10; x++)
{
   if(x == 3)
     continue;
   else       
     DoIterativeWork();       
}

Switch:

转变:

switch(a)
{
 default:{ System.out.println("default"); break;}
 case 'b':{ System.out.println(a); break;}
 case 'a':{ System.out.println(a);}
}