[英]How to use Switch case In the Java
我正在使用Neatbeans 7.0进行Java编程。 我写了一个Switch案例来选择程序。
switch(menu)
{
case 1:
{
//stmt
}
default:
{
//stmt
return;
}
}
我在switch(menu)
时遇到编译时错误switch(menu)
The Error is "strings in switch are not supported in -source 1.6
(use -source 7 or higher to enable strings in switch)
(Alt-Enter显示提示)“任何人都可以帮助我。
我在这里假设menu
是一个字符串类型,虽然它与你的case语句不匹配。 在Java 7中添加了允许在switch
语句中使用字符串。
您可以切换到Java 7并修复case
以便它检查字符串而不是整数1
,或将menu
转换为整数并检查,例如使用Integer.ParseInt()
,类似于:
String menu = "1";
int menuint;
try {
menuint = Integer.ParseInt (menu);
} catch (NumberFormatException e) {
menuint = -1;
}
switch (menuint) {
:
交换机使用byte,short,char和int原始数据类型。
在Java-6之前不支持String。 Java 7支持switch-case中的String对象。
在Java SE 7及更高版本中,您可以在switch语句的表达式中使用String对象。
在这里阅读更多内容: http : //docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
我认为它应该是:
case "1":
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.