英文:
IntelliJ indicating that enhanced switch block doesn't work despite using Java 14
问题
我正在尝试使用在Java 14中标准化的增强switch块。旧版本如下:
switch (expression) {
case OPTION1:
foo();
break;
case OPTION2:
bar();
break;
}
但是由于我使用的是Java 14,我应该能够使用这个:
switch (expression) {
case OPTION1 -> foo();
case OPTION2 -> bar();
}
这是一个最小可复现示例(MRE):
public class Main {
public static void main(String[] args) {
String demo = Math.random() > 0.5 ? "Hello" : "World";
switch (demo) {
case "Hello" -> System.out.println("字符串包含Hello");
case "World" -> System.out.println("字符串包含World");
}
}
}
当我编写该代码时,IntelliJ会给我以下警告:
不支持语言级别14的增强'switch'块
然而,尽管这个警告被突出显示为编译时错误,但这是不正确的。该代码可以正确编译,并且switch块按预期工作,因为我正在使用Java 14。我为什么会收到这个警告?
编辑:根据请求的项目结构
编辑:我的iml文件
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$USER_HOME$/Downloads/MathParser.org-mXparser-v.4.4.0/MathParser.org-mXparser-v.4.4.0/bin/jdk13/MathParser.org-mXparser-v.4.4.0-jdk13.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
英文:
I am trying to use the enhanced switch block that was standardized in Java 14. The old version is this:
switch (expression) {
case OPTION1:
foo();
break;
case OPTION2:
bar();
break;
}
however since I am using Java 14, I should be able to use this:
switch (expression) {
case OPTION1 -> foo();
case OPTION2 -> bar();
}
Here is a MRE:
public class Main {
public static void main(String[] args) {
String demo = Math.random() > 0.5 ? "Hello": "World";
switch (demo) {
case "Hello" -> System.out.println("The string contained Hello");
case "World" -> System.out.println("The string contained World");
}
}
}
When I write that code, IntelliJ gives me the following warning:
> Enhanced 'switch' blocks are not supported at language level '14'
However, that warning, despite being highlighted in red to indicate a compile time error, is incorrect. The code compiles correctly and the switch block works as intended because I am using Java 14. Why am I getting this warning?
Edit: Project structure per request
Edit: My iml file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$USER_HOME$/Downloads/MathParser.org-mXparser-v.4.4.0/MathParser.org-mXparser-v.4.4.0/bin/jdk13/MathParser.org-mXparser-v.4.4.0-jdk13.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
答案1
得分: 1
Here's the translated content:
首先使用IntelliJ 2020
通常造成这种情况的原因是SDK、JDK和语言级别不一致,
在项目结构的项目选项卡中检查项目和模块是否一致,如下所示(好的,我正在使用语言级别11)
项目显示openjdk-14
ide插入了java14.iml文件内容
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_14" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/com" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
英文:
First use intellij 2020
Normally the reason for this is the sdk, jdk and language level are not aligned,
Check in the project structure project tab that Project and Modules are consistent see below (ok I am using language level 11)
ide inserted java14.iml file contents
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_14" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/com" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
答案2
得分: 0
I have tried this and it is working just fine I think you might forget to add the package name
package com.company;
public class Main
{
public static void main(String[] args) {
String demo = Math.random() > 0.5 ? "Hello" : "World";
switch (demo)
{
case "Hello" -> System.out.println("The string contained Hello");
case "World" -> System.out.println("The string contained World");
}
}
}
This is a screenshot for my project structure is try to compare it with yours
英文:
I have tried this and it is working just fine I think you might forget to add the package name
package com.company;
public class Main
{
public static void main(String[] args) {
String demo = Math.random() > 0.5 ? "Hello": "World";
switch (demo)
{
case "Hello" -> System.out.println("The string contained Hello");
case "World" -> System.out.println("The string contained World");
}
}
}
This is a screenshot for my project structure is try to compare it with yours
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论