为什么我不能在 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
Why can't I use 'continue' inside a switch statement in Java?
提问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
continue
inside switch??!!! only break
can 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 break
instead of continue
?
你不应该使用break
代替continue
吗?
回答by Scott Smith
You are using continue
where you should be using break
您正在使用continue
您应该使用的地方break
回答by T.J. Crowder
Because you have a continue
outside of a loop. continue
is 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 switch
case block is the keyword break
(see below).
因为你有continue
一个循环之外。continue
用于跳回到循环的开头,但该代码中没有任何循环。你想要打破switch
case 块的是关键字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 default
condition 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
continue
simply moves directly to the next iteration of the loop.
continue
简单地直接移动到循环的下一次迭代。
break
is 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);}
}