[英]Can I use the same number for 2 cases in old versions in Java?
Can I use the same number for 2 cases in a switch statement in old versions in Java? Java中旧版本的switch语句中的2个case是否可以使用相同的数字?
int rand = 3;
switch(rand)
{
case 3: System.out.print("***");
case 3: System.out.print("###");
default: System.out.println("&&&");
}
I am using the Java version 15.0.1 and it is not working.我使用的是 Java 版本 15.0.1,但它不工作。
This will not be possible as switch needs a single option for a particular input.这是不可能的,因为 switch 需要一个特定输入的选项。 You can either use one option like below.
您可以使用如下所示的一种选项。
int rand = 3;
switch(rand)
{
case 3: System.out.print("***");
default: System.out.println("&&&");
}
or concatenate both together like below或将两者连接在一起,如下所示
int rand = 3;
switch(rand)
{
case 3: System.out.print("***###");
default: System.out.println("&&&");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.