Using two values for one switch case statement
在我的代码中,程序根据用户输入的文本执行某些操作。我的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | switch (name) { case text1: { //blah break; } case text2: { //blah break; } case text3: { //blah break; } case text4: { //blah break; } |
但是,案例
1 2 3 4 | case text1||text4: { //blah break; } |
我知道
您可以使用下面的两个have
1 2 3 4 5 | case text1: case text4:{ //blah break; } |
请参见此示例:代码示例计算特定月份中的天数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | class SwitchDemo { public static void main(String[] args) { int month = 2; int year = 2000; int numDays = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; break; case 4: case 6: case 9: case 11: numDays = 30; break; case 2: if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) numDays = 29; else numDays = 28; break; default: System.out.println("Invalid month."); break; } System.out.println("Number of Days =" + numDays); } } |
这是代码的输出:
1 |
FALLTHROUGH:
Another point of interest is the break statement. Each break statement
terminates the enclosing switch statement. Control flow continues with
the first statement following the switch block. The break statements
are necessary because without them, statements in switch blocksfall : All statements after the matching case label are executed in
through
sequence, regardless of the expression of subsequent case labels,
until a break statement is encountered.
示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | public class SwitchFallThrough { public static void main(String[] args) { java.util.ArrayList<String> futureMonths = new java.util.ArrayList<String>(); int month = 8; switch (month) { case 1: futureMonths.add("January"); case 2: futureMonths.add("February"); case 3: futureMonths.add("March"); case 4: futureMonths.add("April"); case 5: futureMonths.add("May"); case 6: futureMonths.add("June"); case 7: futureMonths.add("July"); case 8: futureMonths.add("August"); case 9: futureMonths.add("September"); case 10: futureMonths.add("October"); case 11: futureMonths.add("November"); case 12: futureMonths.add("December"); default: break; } if (futureMonths.isEmpty()) { System.out.println("Invalid month number"); } else { for (String monthName : futureMonths) { System.out.println(monthName); } } } } |
这是代码的输出:
1 2 3 4 5 | August September October November December |
在switch语句中使用字符串
In Java SE 7 and later, you can use a String object in the switch
statement's expression. The following code example, ,
displays the number of the month based on the value of the String
named month:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | public class StringSwitchDemo { public static int getMonthNumber(String month) { int monthNumber = 0; if (month == null) { return monthNumber; } switch (month.toLowerCase()) { case"january": monthNumber = 1; break; case"february": monthNumber = 2; break; case"march": monthNumber = 3; break; case"april": monthNumber = 4; break; case"may": monthNumber = 5; break; case"june": monthNumber = 6; break; case"july": monthNumber = 7; break; case"august": monthNumber = 8; break; case"september": monthNumber = 9; break; case"october": monthNumber = 10; break; case"november": monthNumber = 11; break; case"december": monthNumber = 12; break; default: monthNumber = 0; break; } return monthNumber; } public static void main(String[] args) { String month ="August"; int returnedMonthNumber = StringSwitchDemo.getMonthNumber(month); if (returnedMonthNumber == 0) { System.out.println("Invalid month"); } else { System.out.println(returnedMonthNumber); } } } |
此代码的输出为8。
从Java文档
你可以这样做:
1 2 3 4 5 | case text1: case text4: { //blah break; } |
1 2 3 4 | case text1: case text4: //blah break; |
注意,支架是多余的。
只要做
1 2 3 | case text1: case text4: do stuff; break; |
其他人的回答是好的。
然而,另一种方法是从案例陈述的内容中提取方法,然后从每个案例中调用适当的方法。
在下面的示例中,大小写"text1"和"text4"的行为相同:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | switch (name) { case text1: { method1(); break; } case text2: { method2(); break; } case text3: { method3(); break; } case text4: { method1(); break; } |
我个人认为这种编写case语句的风格更易于维护,也更易于阅读,特别是当您调用的方法具有良好的描述性名称时。
括号是不必要的。只要做
1 2 3 4 5 6 7 | case text1: case text4: doSomethingHere(); break; case text2: doSomethingElse() break; |
如果有人好奇的话,这就是所谓的失败案例。这样做的能力正是
失败的方法是我感觉最好的方法。
1 2 3 4 5 | case text1: case text4: { //Yada yada break; } |
通过在JDK-12早期访问构建中集成jep 325:switch表达式(preview),现在可以使用新形式的switch标签作为:
1 2 3 | case text1, text4 -> { //blah } |
或者从其中一个答案重新表述演示,比如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public class RephraseDemo { public static void main(String[] args) { int month = 9; int year = 2018; int numDays = 0; switch (month) { case 1, 3, 5, 7, 8, 10, 12 ->{ numDays = 31; } case 4, 6, 9, 11 ->{ numDays = 30; } case 2 ->{ if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) numDays = 29; else numDays = 28; } default ->{ System.out.println("Invalid month."); } } System.out.println("Number of Days =" + numDays); } } |
下面是如何使用maven尝试编译JDK12预览功能的方法
case值只是可以共享相同入口点的无代码"goto"点:
case text1:
case text4: {
//Do something
break;
}
注意,支架是多余的。
你可以使用:
1 2 3 | case text1: case text4: do stuff; break; |