switch java中的字符串

发布于 2024-12-06 19:29:04 字数 248 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遥远的绿洲 2024-12-13 19:29:04

我不想这么说,但是:阅读该死的错误消息。它会告诉您确切问题是什么。

您正在 Java 6 模式下编译,而不是在 Java 7 模式下编译。如果您使用 JDK 7 附带的 javac(如错误消息所示),请使用 -source 7 标志:

> javac -source 7 {stuff here}

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:

> javac -source 7 {stuff here}
£冰雨忧蓝° 2024-12-13 19:29:04

用户查询的两个关键点:

  1. 已经安装了 JDK7;和
  2. 使用 Netbeans

因此,netbeans 附带了两个软件包:

  1. 使用 JDK

意味着安装 Netbeans 7 时,它会自动安装 JDK 7 并将 JDK 7 设置为默认库。

  1. 没有JDK

在安装NetBeans 7时,它会在系统中找到JDK,如果找到它也会自动配置库的路径,但会显示有关JDK7的路径和版本的信息。

所有用户都试图给出该查询的正确答案,但没有人能够按照用户要求以正确的方式回答,因为用户也使用 Netbeans。

继续编辑Hemal Pandya版本,还需要配置一件事,那就是

RightClick on  Project > properties > and in the categories option select > source. see the  
**Hemal Pandya** edit version to look at image, the source option is available above the   
Libraries option.

And

then select **Source/Binary Format** form bottom and set it to JDK 7 (= 1.7). this is the exact solution
of user's post and I am 100% sure now String in swich will work

Two key points from the user query:

  1. Already installed JDK7;and
  2. Using Netbeans

So, netbeans came with two package:

  1. With JDK

Means when install the Netbeans 7 it automatically install JDK 7 and set JDK 7 as Default Library.

  1. Without JDK

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

RightClick on  Project > properties > and in the categories option select > source. see the  
**Hemal Pandya** edit version to look at image, the source option is available above the   
Libraries option.

And

then select **Source/Binary Format** form bottom and set it to JDK 7 (= 1.7). this is the exact solution
of user's post and I am 100% sure now String in swich will work
暖伴 2024-12-13 19:29:04

在 NetBeans 中,转到项目的属性并将库中的 java 平台更改为 java 7

In NetBeans, go to the properties of the project and change the java platform to java 7 in libraries

宣告ˉ结束 2024-12-13 19:29:04

我找到了一个解决方案:
如果您使用的是 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!

情绪 2024-12-13 19:29:04

我不使用 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:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文