Java switch 语句中的多个/重复案例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7032814/
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
Multiple/repeating cases in a Java switch statement
提问by Code Jockey
I would like to know how Java handles multiple identical instances of the same case. I think the following makes sense, conceptually:
我想知道 Java 如何处理同一案例的多个相同实例。我认为以下在概念上是有道理的:
switch (someIntegerValue)
{
case 1:
case 2:
DoSomethingForBothCases();
break;
case 3:
DoSomethingUnrelated();
break;
case 1:
DoSomethingForCase1ThatReliesUponExecutionOfTheEarlierFunctionCall();
break;
case 2:
DoSomethingForCase2ThatReliesUponExecutionOfTheEarlierFunctionCall();
break;
}
Essentially, I would like to have a chunk of code executed for either case 1 or 2 (using fall-through), but then later on, have a chunk of code only executed for case 2.
本质上,我希望为案例 1 或案例 2 执行一段代码(使用 fall-through),但稍后,只为案例 2 执行一段代码。
Rather, is the following necessary, instead?
相反,以下是必要的吗?
switch (someIntegerValue)
{
case 1:
DoSomethingForBothCases();
DoSomethingForCase1ThatReliesUponExecutionOfTheEarlierFunctionCall();
break;
case 2:
DoSomethingForBothCases();
DoSomethingForCase2ThatReliesUponExecutionOfTheEarlierFunctionCall();
break;
case 3:
DoSomethingUnrelated();
break;
}
My actual code is more complex, but would use the same principle (i.e. something like "case 1: nope; alright... case 2: yep! execute this code!; case 3: nope; case 1 again?: still nope!; case 2 again?: yep! execute this code; no more cases: All Done!")
我的实际代码更复杂,但会使用相同的原则(即类似于“案例 1:不;好吧...案例 2:是的!执行此代码!;案例 3:不;再次案例 1?:仍然不! ;再次出现案例 2?:是的!执行此代码;没有更多案例:全部完成!”)
采纳答案by Geoff
You cannot repeat cases in a Java switch statement, it is a compile error. You will need to do as you have suggested, which actually looks like a good factoring.
您不能在 Java switch 语句中重复 case,这是一个编译错误。你需要按照你的建议去做,这实际上看起来是一个很好的保理。
回答by Bohemian
Anything wrong with two switch statements?
两个 switch 语句有什么问题吗?
switch (someIntegerValue) {
case 1:
case 2:
DoSomethingForBothCases();
break;
case 3:
DoSomethingUnrelated();
break;
}
switch (someIntegerValue) {
case 1:
DoSomethingForCase1ThatReliesUponExecutionOfTheEarlierFunctionCall();
break;
case 2:
DoSomethingForCase2ThatReliesUponExecutionOfTheEarlierFunctionCall();
break;
}
That's what I would do.
这就是我会做的。
回答by HJM
You won't be able to do it. You can't have duplicate cases, the compiler will throw a hissy fit lol. I understand your logic in that you would like to check each case and then continue on and check other cases, but the break statement would pull you out of the switch-case statement anyways. What I would recommend is that you consider using a loop (i.e. for, while) if you want to check different things continuously. Coupling if statements with a loop will help you to execute bits of code before others.
你将无法做到。你不能有重复的情况,编译器会发出嘶嘶声,哈哈。我理解您的逻辑,因为您想检查每个案例,然后继续检查其他案例,但是 break 语句无论如何都会将您从 switch-case 语句中拉出来。如果您想连续检查不同的内容,我建议您考虑使用循环(即 for、while)。将 if 语句与循环耦合将帮助您在其他代码之前执行一些代码。
Or if you really like the switch statements, you can create multiple switch-cases so that certain things happen before others.
或者,如果您真的喜欢 switch 语句,您可以创建多个 switch-case,以便某些事情先于其他事情发生。
回答by dnaatwork.com
How About this? NOTE: I don't know that much Java, only Swift 2
这个怎么样?注意:我不太了解 Java,只了解 Swift 2
var someIntegerValue = 1
func someSwitch(){
switch (someIntegerValue) {
case 1:
break;
case 2:
DoSomethingForBothCases();
break;
case 3:
DoSomethingUnrelated();
break;
}
}
where you have a two button actions,
你有两个按钮动作,
some action button NEXT
一些动作按钮 NEXT
someIntegerValue +=1 // changes someIntegerValue to next case
someIntegerValue() //loads switch
some action button 2 Back
一些动作按钮 2 返回
someIntegerValue -=1 // changes someIntegerValue to next case
someIntegerValue() //loads switch