为什么我不能使用“继续”? Java 中的 switch 语句内?
为什么下面的代码
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");
}
}
会报错:
在循环外继续
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
失败是 switch 语句的标准行为,因此,在 switch 语句中使用 continue 没有意义。 continue 语句仅在 for/while/do..while 循环中使用。
根据我对您意图的理解,您可能想写:
我还建议您将默认条件放在最后。
编辑:
continue 语句不能在 switch 语句内使用,这并不完全正确。 (理想情况下标记的)继续语句是完全有效的。例如:
这将产生以下输出:
0
2
4
6
8
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.
Based on my understanding of your intentions, you probably want to write:
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) continue statement is entirely valid. For example:
This will produce the following output:
0
2
4
6
8
continue 语句可以在循环中使用,但不能在 switch 中使用。您可能想要的是
休息
。The
continue
-Statement may be used in loops and not in switch. What you probably want is abreak
.因为你在循环之外有一个
continue
。continue
用于跳转到循环的下一次迭代,跳过当前迭代的剩余部分。但该代码中没有任何循环。您想要打破switch
case 块的是关键字break
(见下文)。也没有必要将每个 case 块放在大括号内(除非您希望在其中包含本地范围的变量)。
所以有点像这样的东西会更标准:
还有一种思想认为
default
条件应该始终放在最后。这不是一个要求,只是一个相当广泛使用的约定。Because you have a
continue
outside of a loop.continue
is for jumping to the next iteration of a loop, skipping the remainder of the current iteration. But you don't have any loop in that code. What you want for breaking out of aswitch
case block is the keywordbreak
(see below).There's also no need to put every case block within braces (unless you want locally-scoped variables within them).
So something a bit like this would be more standard:
There's also a school of thought that says the
default
condition should always be at the end. This is not a requirement, just a fairly widely-used convention.难道不应该使用
break
而不是continue
吗?Shouldn't you use
break
instead ofcontinue
?continue
只是直接移动到循环的下一次迭代。break
用于跳出循环和开关。使用
break;
而不是continue;
继续:
切换:
continue
simply moves directly to the next iteration of the loop.break
is used to break out of loops and switches.Use
break;
instead ofcontinue;
Continue:
Switch:
您在应该使用
break
的地方使用了continue
You are using
continue
where you should be usingbreak
在开关内
继续
???!!!只有break
可以保留在 switch 内部。!你的代码应该是,
continue
inside switch??!!! onlybreak
can be kept inside switch.!ur code should be,