package com.learningjava.conditionals;

public class SwitchExcercises {

    public static boolean isWeekDay(int dayNumber) {
        switch (dayNumber) {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
            return true;
        case 5:
        case 6:
            return false;
        case default:
            return false;
        }
    }
}

我正在使用Eclipse作为我的ide.在大小写默认旁边显示错误,表示

The preview feature Pattern Matching in Switch is only available with source level 20 and above.

使用Java-se 17.谢谢你的帮助.

推荐答案

这是一个语法错误,您原本打算编写的代码实际上试图做一些与您的预期完全不同的事情.您的语法错误意外地与一个名为Pattern Matching for switch Expressions and Statements的全新Java特性重叠.

要修复错误,只需将case default切换为default即可.

case default:
    return false;

default:
    return false;

And 至 better explain the error message, Eclipse the IDE is aware of the new Java feature, and thus, is trying 至 let you know that what you are doing looks like the new feature, and that if you are trying 至 do the new feature, you should upgrade your code level 至 21 or above.

但是,当然,Eclipse弄错了--您只需要一个默认子句.因此,按照我上面展示的那样编写,错误应该会消失.

Java相关问答推荐

将Nimbus设置为计算机上运行的所有Java应用程序的默认外观

Listview—在Android Java中正确链接项目时出错

Apache POI:使用反射获取zoom 级别

Spring Boot 3.2.2中的@Inject和@Resource Remove

Hibernate EmptyInterceptor可以工作,但不能拦截器

对Java中的通配符参数的混淆

为什么我的在一个范围内寻找素数的程序不能像S所期望的那样工作

暂停计时器

Java中将文本拆分为数字或十进制数字和字符串

在处理2个映射表时,没有更多的数据可从套接字读取

JNI:将代码打包成自包含的二进制文件

无法使用Freemarker从XML中读取重复的标记值

如何在Java springboot中从一个端点发送多个时间响应?

Android Studio模拟器没有互联网

在Spring Boot JPA for MySQL中为我的所有类创建Bean时出错?

在应用getCellFormula()时,Excel引用中的文件名始终为";[1]";使用Apache POI()

Java泛型方法重载

在输入端没有可行的替代方案'; Select *';

SonarQube在合并升级到java17后对旧代码提出错误

Jackson YAML:支持锚点扩展/覆盖