简体   繁体   English

不能在Java 1.8和Eclipse中切换大小写?

[英]switch-case in Java 1.8 with Eclipse not possible?

I have re-installed my Java 8 and get a new and fresh Eclipse version. 我已经重新安装了Java 8,并获得了一个全新的Eclipse版本。

If I write the following code: 如果我编写以下代码:

String a = "001100";

switch(a) {
   case "5": a = "25"; break;
   default: System.exit(0);
}

then Eclipse underlines the "a" in the switch-brackets red and says "Change project compliance and JRE to 1.7". 然后Eclipse在开关括号中的红色下划线“ a”,并说“将项目合规性和JRE更改为1.7”。 I have not tried this because I'm using Java 1.8 and this should work? 我没有尝试过此操作,因为我正在使用Java 1.8,这应该可以工作吗? I don't want to use Java 1.7. 我不想使用Java 1.7。

My Eclipse is setting up with Java 1.8, also this Project. 我的Eclipse正在使用Java 1.8(也是该项目)进行设置。 There is no lower Java version in the settings.... 设置中没有更低的Java版本。

Where is the problem? 问题出在哪儿?

在此处输入图片说明

according to this answer switching for strings is implemented since JDK7 so switching to 1.7 compliance should do it. 根据此答案 ,自JDK7起就实现了字符串切换,因此应切换到1.7兼容性。 As I understand this is not 'switching back' your java version instead it enables you to use those 1.7 features. 据我了解,这不是“切换回”您的Java版本,而是使您能够使用这些1.7功能。

The problem most probably is that your language level is set below java 1.7. 问题很可能是您的语言级别设置为低于Java 1.7。 It is not the same as JRE version used by project. 它与项目使用的JRE版本不同。 try to find language level in your project settings and set it to Java or Java 7 for this switch on String to be working 尝试在项目设置中找到语言级别,并将其设置为Java或Java 7,以使String上的此开关起作用

Your Eclipse is using a Java SDK version lower than 1.7. 您的Eclipse使用的Java SDK版本低于1.7。 Check your Java compiler level in Eclipse: 在Eclipse中检查您的Java编译器级别:

Windows>Preferences>Java>Compiler>compiler compliance level Windows>首选项> Java>编译器>编译器合规性级别

You must set the default JDK version for the eclipse, to take all the projects to be created in default JDK version eclipse. 您必须为Eclipse设置默认的JDK版本,以使所有项目都在默认的JDK版本eclipse中创建。

to know how to set the default JDK version in eclipse click here 要知道如何在Eclipse中设置默认的JDK版本, 请单击此处

Strings in switch statements are available only post JDK 1.7 switch语句中的字符串仅在JDK 1.7之后可用

https://docs.oracle.com/javase/7/docs/technotes/guides/language/strings-switch.html https://docs.oracle.com/javase/7/docs/technotes/guides/language/strings-switch.html

You need to check your java compiler level and change it to JDK 1.8 or 1.7. 您需要检查Java编译器级别并将其更改为JDK 1.8或1.7。

(Right Click on project, go to preferences then look for "Java Compiler" and change "Compiler Compliance Level") (右键单击项目,转到首选项,然后查找“ Java Compiler”并更改“ Compiler Compliance Level”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM