java 开关java中的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7564334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:31:46  来源:igfitidea点击:

String in switch java

javajava-7

提问by Shahid Ghafoor

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

-source 1.6 不支持 switch 中的字符串,使用 -source 7 或更高版本来启用 switch 中的字符串

So, I have already installed JDK7, Why compiler show this message and giving an error while on compiling?

所以,我已经安装了 JDK7,为什么编译器在编译时显示此消息并给出错误?

I am using NetBeans

我正在使用 NetBeans

回答by Matt Ball

I hate to say it, but: read the darn error message.It tells you exactlywhat the problem is.

我不想这么说,但是:阅读该死的错误消息。它会准确地告诉您问题出在哪里。

You are compiling in Java 6 mode, not compiling in Java 7 mode. If you're using javacthat comes with JDK 7, as the error message says, use the -source 7flag:

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

> javac -source 7 {stuff here}

回答by Both FM

Two key points from the user query:

用户查询的两个关键点:

  1. Already installed JDK7;and
  2. Using Netbeans
  1. 已经安装了JDK7;并且
  2. 使用 Netbeans

So, netbeans came with two package:

所以,netbeans 附带了两个包:

  1. With JDK
  1. 使用 JDK

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

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

  1. Without JDK
  1. 没有 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.

在NetBeans 7的安装过程中,发现系统中有JDK,如果发现它也会自动配置库的路径,但会显示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.

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

With continue the edit version of Hemal Pandya, one thing more is required to configure, which is that

随着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

回答by Samrat

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

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

回答by Antonio Jimenez

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

我找到了一个解决方案:如果您使用的是 Netbeans 7.1(甚至 7.0),只需按照以下步骤操作:右键单击项目 -> 属性 -> 源 ->(在底部)源/二进制格式

Just select JDK 7, and that's all, you can then use strings in switch!

只需选择JDK 7,仅此而已,然后您就可以在switch 中使用字符串了!

回答by Miserable Variable

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.

我不使用 NetBeans,但似乎有一个默认为 1.6 的合规性开关。正如其他人指出的那样,您必须找到该开关并将其设置为 7。

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:

编辑:我发现我找到了 netbeans.org/kb/docs/java/project-setup.html#projects-jdk。你好像已经完成了注册jdk的第二步。但也许它不是默认值?按照说明切换标准项目的目标JDK。查看图像似乎在此对话框中:

enter image description here

在此处输入图片说明