Syn*_*nia 89 java if-statement switch-statement
在Java中是否可以编写一个switch语句,其中每个case包含多个值?例如(虽然以下代码显然不起作用):
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;
}
Run Code Online (Sandbox Code Playgroud)
我认为这可以在Objective C中完成,Java中有类似的东西吗?或者我应该只使用if,else if语句呢?
mis*_*tor 94
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");
}
Run Code Online (Sandbox Code Playgroud)
Jef*_*rey 74
你最接近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;
}
Run Code Online (Sandbox Code Playgroud)
使用if陈述.
Hay*_*man 28
另一种方法是通过除以它来使用数学运算,例如:
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;
}
Run Code Online (Sandbox Code Playgroud)
但是,正如您所看到的,只有在每种情况下固定范围时才能使用此功能.
bli*_*zen 12
我认为你不能用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:
//
}
Run Code Online (Sandbox Code Playgroud)
A_A*_*old 10
从 Java 12 开始支持它。查看JEP 354。这里没有“范围”的可能性,但也很有用。
switch (day) {
case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);//number of letters
case TUESDAY -> System.out.println(7);
case THURSDAY, SATURDAY -> System.out.println(8);
case WEDNESDAY -> System.out.println(9);
}
Run Code Online (Sandbox Code Playgroud)
您也应该能够在整数上实现它。请注意,您的 switch 语句必须详尽无遗(使用default关键字,或在 case 语句中使用所有可能的值)。
更新: 从 Java 20(23 年 3 月)开始,这是可能的!查看JEP 433。他们的例子是如果您想测试特定大小的三角形。
switch (s) {
case null ->
{ break; }
case Triangle t
when t.calculateArea() > 100 ->
System.out.println("Large triangle");
default ->
System.out.println("A shape, possibly a small triangle");
}
Run Code Online (Sandbox Code Playgroud)
小智 9
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");
Run Code Online (Sandbox Code Playgroud)
阅读所有评论后,我没有看到任何人提到增强型开关,在这种情况下,您可以在一种情况下拥有多个值,如下所示 ->
switch(value){
case 1,2,3,4:
//dosth
break;
case 7,9,10,23:
//dosth
break;
}
Run Code Online (Sandbox Code Playgroud)
由于在您的情况下,每种情况下只有一个表达式,因此您可以执行以下操作,而不需要break每种情况 ->
switch (value) {
case 1, 2, 3, 4 -> System.out.println("one of 1,2,3,4 matched");
case 7, 9, 10, 23 -> System.out.println("one of 7,9,10,23 matched");
}
Run Code Online (Sandbox Code Playgroud)
这是 java 中增强开关的众多附加好处之一。
如果您必须使用开关,请尝试此操作。
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;
}
}
Run Code Online (Sandbox Code Playgroud)
或者您可以按预期使用您的单独案例并使用您的默认案例将范围说明指定为:
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;
}
Run Code Online (Sandbox Code Playgroud)
使用一个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);
Run Code Online (Sandbox Code Playgroud)
我知道这篇文章很旧,但是我相信这个答案值得认可。无需避免使用switch语句。这可以在Java中完成,但是可以通过switch语句完成,而不是通过case来完成。它涉及使用三元运算符。
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = Integer.parseInt(sc.nextLine());
switch ((1 <= num && num <= 5 ) ? 0 :
(6 <= num && num <= 10) ? 1 : 2) {
case 0:
System.out.println("I'm between one and five inclusive.");
break;
case 1:
System.out.println("I'm between 6 and 10 inclusive.");
break;
case 2:
System.out.println("I'm not between one and five or 6 and 10 inclusive.");
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)