Java:我可以在 switch 语句中只遇到一种情况吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15606572/
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
Java: Can I fall through only one case in a switch statement
提问by stephenwade
In Java, can I fall through only one of the cases in a switch
statement? I understand that if I break
, I will fall through to the end of the switch
statement.
在 Java 中,我是否可以仅通过switch
语句中的一种情况?我明白,如果我break
,我将失败到switch
语句的结尾。
Here's what I mean. Given the following code, on case 2, I want to execute case 2 and case 1. On case 3, I want to execute case 3 and case 1, but not case 2.
这就是我的意思。给定以下代码,在案例 2 上,我想执行案例 2 和案例 1。在案例 3 上,我想执行案例 3 和案例 1,但不执行案例 2。
switch(option) {
case 3: // code
// skip the next case, not break
case 2: // code
case 1: // code
}
采纳答案by nicholas.hauschild
No, what you are after is not possible with a switch
statement. You will fall through each case
until you hit a break
. Perhaps you want case 1
to be outside of your switch
statement, so that it is executed regardless.
不,您所追求的内容无法通过switch
声明实现。您将通过每一个,case
直到您击中break
. 也许你想case 1
在你的switch
语句之外,以便它无论如何都会被执行。
回答by m0skit0
Put the code into methods and call as appropriate. Following your example:
将代码放入方法中并适当调用。按照你的例子:
void case1() {
// Whatever case 1 does
}
void case2() {
// Whatever case 2 does
}
void case3() {
// Whatever case 3 does
}
switch(option) {
case 3:
case3();
case1();
break;
case 2:
case2();
case1();
break;
case 1:
case1(); // You didn't specify what to do for case 1, so I assume you want case1()
break;
default:
// Always a good idea to have a default, just in case demons are summoned
}
Of course case3()
, case2()
... are very poor method names, you should rename to something more meaningful about what the method actually does.
当然case3()
,case2()
... 是非常糟糕的方法名称,您应该将其重命名为对方法实际执行的操作更有意义的名称。
回答by millimoose
My suggestion is to not use fallthrough for anythingexcept cases like the following:
我的建议是,除了以下情况外,不要对任何事情使用 fallthrough :
switch (option) {
case 3:
doSomething();
break;
case 2:
case 1:
doSomeOtherThing();
break;
case 0:
// do nothing
break;
}
That is, giving several cases the exactsame block of code to handle them (by "stacking" the case
labels), making it more or less obvious what the flow is here. I doubt most programmers intuitively check for case fall through (because the indentation makes a case look like as a proper block) or can efficiently read code that relies on it - I know I don't.
也就是说,给几个案例提供完全相同的代码块来处理它们(通过“堆叠”case
标签),使这里的流程或多或少变得显而易见。我怀疑大多数程序员会凭直觉检查 case fall through(因为缩进使 case 看起来像一个适当的块)或者可以有效地读取依赖它的代码 - 我知道我不知道。
回答by ZhongYu
switch(option)
{
case 3:
...
break;
case 2:
...
break;
}
... // code for case 1
回答by ceruleus
Something like this maybe.
也许像这样的事情。
switch(option) {
case 3: // code
// skip the next case, not break
// BLOCK-3
case 2: // code
if(option == 3) break;
// BLOCK-2
case 1: // code
// BLOCK-1
}
回答by gerrytan
In switch statement if you don't break
the subsequent case is executed. To give you simple example
在 switch 语句中,如果不break
执行后续 case。给你简单的例子
int value = 2;
switch(value) {
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
case 3:
System.out.println("three");
break;
}
Will output
会输出
two
three
Because break
wansn't executed on case 2
因为break
没有在案例 2 上执行