switch java中的字符串
switch(title)
{
case "Return":
returnBooks();
break;
}
-source 1.6 不支持 switch 中的字符串,请使用 -source 7 或更高版本在 switch 中启用字符串
所以,我已经安装了 JDK7,为什么编译器在编译时显示此消息并给出错误?
我正在使用 NetBeans
switch(title)
{
case "Return":
returnBooks();
break;
}
strings in switch are not supported in -source 1.6, use -source 7 or higher to enable strings in switch
So, I have already installed JDK7, Why compiler show this message and giving an error while on compiling?
I am using NetBeans
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不想这么说,但是:阅读该死的错误消息。它会告诉您确切问题是什么。
您正在 Java 6 模式下编译,而不是在 Java 7 模式下编译。如果您使用 JDK 7 附带的
javac
(如错误消息所示),请使用-source 7
标志:I hate to say it, but: read the darn error message. It tells you exactly what the problem is.
You are compiling in Java 6 mode, not compiling in Java 7 mode. If you're using
javac
that comes with JDK 7, as the error message says, use the-source 7
flag:用户查询的两个关键点:
因此,netbeans 附带了两个软件包:
意味着安装 Netbeans 7 时,它会自动安装 JDK 7 并将 JDK 7 设置为默认库。
在安装NetBeans 7时,它会在系统中找到JDK,如果找到它也会自动配置库的路径,但会显示有关JDK7的路径和版本的信息。
所有用户都试图给出该查询的正确答案,但没有人能够按照用户要求以正确的方式回答,因为用户也使用 Netbeans。
继续编辑Hemal Pandya版本,还需要配置一件事,那就是
And
Two key points from the user query:
So, netbeans came with two package:
Means when install the Netbeans 7 it automatically install JDK 7 and set JDK 7 as Default Library.
On the installation of NetBeans 7, it found JDK in the system, and if found it automatically configure the path of library as well, but show you information about the path and version of JDK7.
All of users try to give the correct answer of this query, but no one is in position to answer in the correct way as the user required because user using the Netbeans as well.
With continue the edit version of Hemal Pandya, one thing more is required to configure, which is that
And
在 NetBeans 中,转到项目的属性并将库中的 java 平台更改为 java 7
In NetBeans, go to the properties of the project and change the java platform to java 7 in libraries
我找到了一个解决方案:
如果您使用的是 Netbeans 7.1(甚至 7.0),则只需遵循以下步骤:
右键单击项目 ->属性->来源-> (在底部) Source/Binary Format
只需选择JDK 7即可,然后就可以在switch中使用字符串了!
I have found a solution:
If you are using Netbeans 7.1 (even, 7.0), just you have to follow this:
Right Click on Project -> Properties -> Sources -> (On the bottom) Source/Binary Format
Just select JDK 7, and that's all, you can then use strings in switch!
我不使用 NetBeans,但似乎有一个默认为 1.6 的合规性开关。正如其他人指出的那样,您必须找到该开关并将其设置为 7。
编辑:我发现我找到了 netbeans.org/kb/docs/java/project-setup.html#projects-jdk。你好像已经完成了注册jdk的第二步了。但也许这不是默认的?按照切换标准项目的目标 JDK 的说明进行操作。查看图像似乎在此对话框中:
I do not use NetBeans but there seems to be a compliance switch that has defaulted to 1.6. You will have to find that switch and set it to 7, as others have pointed out.
EDIT: I found I found netbeans.org/kb/docs/java/project-setup.html#projects-jdk. You seem to have done the second step of registering jdk. But maybe it is not the default? Follow the instructions to To switch the target JDK of a standard project. Looking at images it seems to be in this dialog: