Viv*_*mar 1 java case switch-statement
我知道这是一个愚蠢的问题,但我想知道案例标签为什么不采用变量.代码是 -
public class Hello
{
public static void main(String[] args)
{
final int y=9;
int a=1,b=2,c=3;
switch(9)
{
case y:
{
System.out.println("Hello User");
break;
}
case a:
{
System.out.println("Hello World");
break;
}
case b:
{
System.out.println("Buff");
break;
}
default:
{
System.out.println("Yo bitch");
break;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
虽然,我已初始化a,b和c,但它显示错误.为什么?
实际上,它确实需要变量,但它们必须是变量final.
如果你这样做:
final int a = 1, b = 2, c = 3;
Run Code Online (Sandbox Code Playgroud)
然后它会编译得很好.
作为一个侧面说明,有switch (9)和维护列表的case块,并没有多大意义,因为只有的一个case(或多个)实际上是可达到.
| 归档时间: |
|
| 查看次数: |
73 次 |
| 最近记录: |