在Java中,每种情况下使用switch语句的值范围?

在Java中,可以写一个switch语句,其中每个case包含多个值? 例如(尽pipe下面的代码显然不起作用):

switch (num) { case 1 .. 5: System.out.println("testing case 1 to 5"); break; case 6 .. 10: System.out.println("testing case 6 to 10"); break; } 

我认为这可以在Objective C中完成,Java中是否有类似的东西? 或者我应该只使用ifelse if语句呢?

Java没有这种东西。 为什么不只是做以下?

 public static boolean isBetween(int x, int lower, int upper) { return lower <= x && x <= upper; } if (isBetween(num, 1, 5)) { System.out.println("testing case 1 to 5"); } else if (isBetween(num, 6, 10)) { System.out.println("testing case 6 to 10"); } 

switch语句可以得到最接近的那种行为

 switch (num) { case 1: case 2: case 3: case 4: case 5: System.out.println("1 through 5"); break; case 6: case 7: case 8: case 9: case 10: System.out.println("6 through 10"); break; } 

使用if语句。

另一种select是使用math运算来划分它,例如:

 switch ((int) num/10) { case 1: System.out.println("10-19"); break; case 2: System.out.println("20-29"); break; case 3: System.out.println("30-39"); break; case 4: System.out.println("40-49"); break; default: break; } 

但是,正如你所看到的,这只能在范围固定的情况下才能使用。

我不认为你可以在Java中做到这一点。 最好的办法是把代码放在最后一个范围内。

 switch (num) { case 1: case 2: case 3: case 4: case 5: System.Out.Println("testing case 1 to 5"); break; case 6: case 7: case 8: case 9: case 10: System.Out.Println("testing case 6 to 10"); break; default: // } 
  case 1: case 2: case 3: case 4: case 5: System.out.println("testing case 1 to 5"); break; case 6: case 7: case 8: case 9: case 10: System.out.println("testing case 6 to 10"); break; default: System.out.println("default"); 

不,你不能这样做。 你能做的最好的就是

 case 1: case 2: case 3: case 4: case 5: System.Out.Println("testing case 1 to 5"); break; 

在相同的case语句中,可以使用switch语句允许的fall方法对几个条件进行分组,这在Java教程中已经提到,并在§14.11中有详细说明。 Java语言规范 的switch语句 。

以下代码片段摘自教程中的示例,它计算每个月的天数(从第1月到第12月编号):

 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; } 

正如你所看到的,为了在一个case语句中涵盖一系列值,唯一的select是逐个列出每个可能的值。 作为一个额外的例子,下面是如何在问题中实现伪代码:

 switch(num) { case 1: case 2: case 3: case 4: case 5: System.out.println("testing case 1 to 5"); break; case 6: case 7: case 8: case 9: case 10: System.out.println("testing case 6 to 10"); break; } 

你可以使用enum来表示你的范围,

 public static enum IntRange { ONE_TO_FIVE, SIX_TO_TEN; public boolean isInRange(int v) { switch (this) { case ONE_TO_FIVE: return (v >= 1 && v <= 5); case SIX_TO_TEN: return (v >= 6 && v <= 10); } return false; } public static IntRange getValue(int v) { if (v >= 1 && v <= 5) { return ONE_TO_FIVE; } else if (v >= 6 && v <= 10) { return SIX_TO_TEN; } return null; } } 

如果你必须使用开关试试这个。

 public static int range(int num){ if ( 10 < num && num < 20) return 1; if ( 20 <= num && num < 30) return 2; return 3; } public static final int TEN_TWENTY = 1; public static final int TWENTY_THIRTY = 2; public static void main(String[]args){ int a = 110; switch (range(a)){ case TEN_TWENTY: System.out.println("10-20"); break; case TWENTY_THIRTY: System.out.println("20-30"); break; default: break; } } 

@missingfaktor的答案确实是正确的,但有点过于复杂。 代码是更详细的(至less对于连续的时间间隔),那么它可能是,并要求重载/转换和/或参数化长,浮动,整数等

 if (num < 1) System.Out.Println("invalid case: " + num); // you should verify that anyway else if (num <= 5) System.Out.Println("1 to 5"); else if (num <= 10) System.Out.Println("6 to 10"); else if (num <= 42) System.Out.Println("11 to 42"); else System.Out.Println("43 to +infinity"); 

或者,您可以按照预期使用您的个人案例,并使用您的默认情况来指定范围说明:

 switch(n) { case 1 : System.out.println("case 1"); break; case 4 : System.out.println("case 4"); break; case 99 : System.out.println("case 99"); break; default : if (n >= 10 && n <= 15) System.out.println("10-15 range"); else if (n >= 100 && n <= 200) System.out.println("100-200 range"); else System.out.println("Your default case"); break; } 

使用NavigableMap实现,如TreeMap

 /* Setup */ NavigableMap<Integer, Optional<String>> messages = new TreeMap<>(); messages.put(Integer.MIN_VALUE, Optional.empty()); messages.put(1, Optional.of("testing case 1 to 5")); messages.put(6, Optional.of("testing case 6 to 10")); messages.put(11, Optional.empty()); /* Use */ messages.floorEntry(3).getValue().ifPresent(System.out::println); 

这是一个美丽而简约的方式

 (num > 1 && num < 5) ? first_case_method() : System.out.println("testing case 1 to 5") : (num > 5 && num < 7) ? System.out.println("testing case 1 to 5") : (num > 7 && num < 8) ? System.out.println("testing case 1 to 5") : (num > 8 && num < 9) ? System.out.println("testing case 1 to 5") : ... : System.out.println("default");