我们可以在 Java 的同一个 switch 语句中的另一个案例中调用“案例”吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31498571/
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
Can we call a "case" inside another case in the same switch statement in Java?
提问by Lasitha Konara
My intention is to call two cases inside another case in the same switch statement,
我的目的是在同一个 switch 语句中的另一个案例中调用两个案例,
switch (orderType) {
case 1:
statement 1;
break;
case 2:
statement 2;
break;
case 3:
**call case 1;**
**Call case 2;**
break;
default:
break;`
}
Can we do that in Java?
我们可以在 Java 中做到这一点吗?
采纳答案by sandalone
Although you cannot influence the switch
cases directly, you can call switch's parent method from one case and pass different arguments. For example,
虽然您不能switch
直接影响案例,但您可以从一个案例调用 switch 的父方法并传递不同的参数。例如,
void foo(int param1, String param2, ...) {
switch (param1) {
case 0:
foo(1, "some string");
break;
case 1:
//do something
break;
default:
break;
}
}
回答by Seelenvirtuose
No, you can't jumpto the code snippet in another switch case. You can however extract the code into an own method that can be called from another case:
不,您不能跳转到另一个 switch 案例中的代码片段。但是,您可以将代码提取到可以从其他情况调用的自己的方法中:
switch (orderType) {
case 1:
someMethod1();
break;
case 2:
someMethod2();
break;
case 3:
someMethod1();
someMethod2();
break;
default:
break;
}
void someMethod1() { ... }
void someMethod2() { ... }
回答by Mureinik
You can't just call a another case
block like this. What you could do, though, is wrap each of these blocks in a method, and reuse them:
你不能case
像这样调用另一个块。但是,您可以做的是将这些块中的每一个包装在一个方法中,然后重用它们:
private void doSomething() {
// implementation
}
private void doSomethingElse() {
// implementation
}
private void runSwitch(int order) {
switch (orderType) {
case 1:
doSomething();
break;
case 2:
doSomethingElse();
break;
case 3:
doSomething();
doSomethingElse();
break;
default:
break;
}
}
回答by Pavan Kumar
Using methods is the best way to do it as mentioned in the accepted answer but for some reasons if you are unable to create method, Here is one more way to do this without using methods
如已接受的答案中所述,使用方法是最好的方法,但由于某些原因,如果您无法创建方法,这是不使用方法的另一种方法
boolean isBreakTheLoop = false, isCase2Required = false;
while(!isBreakTheLoop){
switch (orderType) {
case 1:
statement 1;
if(isCase2Required){
orderType = 2;
break;
}
isBreakTheLoop = true;
break;
case 2:
statement 2;
isBreakTheLoop = true;
break;
case 3:
orderType = 1;
isCase2Required = true;
break;
default:
isBreakTheLoop = true;
break;
}
}
回答by Ebony Maw
As others have pointed out, you can implement your switch cases inside a method which takes a parameter, which corresponds to the desired switch case. These switch cases can recursively call the same method, with the desired switch cases. Here is a fully-working example which allows you to check if a year is a leap year, or not:
正如其他人指出的那样,您可以在一个带有参数的方法中实现您的开关案例,该参数对应于所需的开关案例。这些 switch case 可以递归调用相同的方法,使用所需的 switch case。这是一个完整的示例,可让您检查一年是否为闰年:
public class LeapYear {
static int year = 2016;
public static void main(String[] args) {
leapSwitch(1);
}
public static void leapSwitch(int switchcase) {
switch (switchcase) {
case 1: {
if (year % 4 == 0) {
leapSwitch(2);
} else {
leapSwitch(5);
}
}
break;
case 2: {
if (year % 100 == 0) {
leapSwitch(3);
} else {
leapSwitch(4);
}
}
break;
case 3: {
if (year % 400 == 0) {
leapSwitch(4);
} else {
leapSwitch(5);
}
}
break;
case 4: {
System.out.println(year+ " is a leap year!");
}
break;
case 5: {
System.out.println("Not a leap year...");
}
break;
}
}
}
回答by Nicolas Facciolo
You can use this trick in some case :
在某些情况下,您可以使用此技巧:
switch (orderType) {
case 3:
statement 3;
case 2:
statement 2;
case 1:
statement 1;
break;
default:
break;`
}
}