黑马程序员 switch 语句使用 String 参数
2015-04-17 17:55
447 查看
当我尝试在 switch 语句使用 String 参数时(注意
提示如下错误:
Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted
意思是说,我的 jre 本版本太低,不支持。据查 在 Java 7之前,switch 只能支持 byte、short、char、int或者其对应的封装类以及 Enum 类型。在 Java 7中,String支持也终于被加上了。
安装 JDK 1.7+,在项目中更改配置引入该 JDK 版本依赖库。
更改 pom.xml 文件,设置 maven-compiler-plugin 插件目标版本为 1.7+,例如
ctrType为字符串)
switch (ctrType) { case "01" : exceptionType = "读FC参数数据"; break; case "03" : exceptionType = "读FC保存的当前表计数据"; break; default: exceptionType = "未知控制码:"+ctrType; }
提示如下错误:
Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted
意思是说,我的 jre 本版本太低,不支持。据查 在 Java 7之前,switch 只能支持 byte、short、char、int或者其对应的封装类以及 Enum 类型。在 Java 7中,String支持也终于被加上了。
解决
普通项目
安装 JDK 1.7+,在项目中更改配置引入该 JDK 版本依赖库。
Maven 项目
更改 pom.xml 文件,设置 maven-compiler-plugin 插件目标版本为 1.7+,例如<plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> ... </plugins>
相关文章推荐
- Java switch 语句使用 String 参数
- Java switch 语句使用 String 参数
- android switch不能使用参数String,View等类的解决方法
- Switch能否使用String做参数
- 在jdk 1.6版本中,switch的参数无法使用String类型
- java中switch能否使用String作为参数?
- idea15写switch语句参数为String类型时冒红解决办法
- 关于switch语句中使用String类型的实现原理
- 关于switch语句中使用String类型的实现原理
- 实现1+2+3...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)
- 实现1+2+3+...+n。要求不能使用乘除法,for,while,if,else,switch,case,等关键字及条件判断语句。
- Java中main方法参数String[ ] args的使用。
- JDK7支持switch中使用String
- JAVA流程控制 IF ELSE语句与Switch Case语句的使用
- Java基础:switch case default 语句的的使用
- 选择 | 循环 | 语句(if,if……else,switch,while,for)的理解和使用
- 1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)
- java switch case 中使用String
- 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)
- 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。