为什么使用 Switch 表达式时不能用一行表达 return?

发布于 2025-01-19 02:56:32 字数 825 浏览 7 评论 0原文

while (true) {
    console.mainMenu();
    String inputCommand = console.input();
    switch(inputCommand) {
        case "exit" -> return;
        case "create" -> {
            Voucher voucher = createVoucher();
            voucherRepository.save(voucher);
        }
        case "list" -> voucherRepository.findByIdAll();
        default -> console.output(Error.INVALID_COMMAND);
    }
}

case "exit" -> 时发生错误return; 在上面的代码中。

在 switch 表达式中,如果代码表示为单行,则可以省略括号,但它仍然要求添加括号。

这是我遇到的编译错误

error: unexpected statement in case, expected is an expression, 
a block or a throw statement case "exit" -> return;

为什么会出现问题?

如果我添加一个支架,效果很好。

但我想知道为什么去掉括号后会出现错误?

while (true) {
    console.mainMenu();
    String inputCommand = console.input();
    switch(inputCommand) {
        case "exit" -> return;
        case "create" -> {
            Voucher voucher = createVoucher();
            voucherRepository.save(voucher);
        }
        case "list" -> voucherRepository.findByIdAll();
        default -> console.output(Error.INVALID_COMMAND);
    }
}

An error occurs in case "exit" -> return; in the above code.

In a switch expression, if the code is expressed as a single line, you can omit the brackets, but it continues to ask for the addition of the brackets.

This is the compilation error I'm getting:

error: unexpected statement in case, expected is an expression, 
a block or a throw statement case "exit" -> return;

Why is the problem happening?

If I add a bracket, it works well.

But I wonder why the error occurs when the brackets are removed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

您的好友蓝忘机已上羡 2025-01-26 02:56:32

您不能在 切换表达式。它必须是表达式或块(可以包含一个或多个语句)

根据规范,这些是有效的规则

切换规则:

  • 开关标签 -> 表达式 ;

  • 开关标签 -> 阻止

  • 开关标签 -> 抛出语句

有效规则示例:

int a = 9;
switch(a) {
    case 1 -> System.out.println(a);                 // Expression
    case 2 -> {return;}                              // Block
    default -> throw new IllegalArgumentException(); // ThrowStatement
}

这些是来自 Oracle 教程.

表达

表达式是由变量运算符方法调用组成的构造,它们被构造根据该语言的语法,其计算结果为单个值。

声明

语句大致相当于自然语言中的句子。一条语句形成一个完整的执行单元。以下类型的表达式可以通过以分号(;)结束表达式来组成语句。

阻止

块是平衡大括号之间的零个或多个语句

return; 不是变量或方法调用,而是声明。。因此,它必须包含在大写字母中以形成代码块。

You can't use a statement as a rule (the part after the arrow ->) in a switch expression. It must be either an expression or block (which can contain a statement or multiple statements)

These are valid rules according to the specification:

SwitchRule:

  • SwitchLabel -> Expression ;

  • SwitchLabel -> Block

  • SwitchLabel -> ThrowStatement

Example of valid rules:

int a = 9;
switch(a) {
    case 1 -> System.out.println(a);                 // Expression
    case 2 -> {return;}                              // Block
    default -> throw new IllegalArgumentException(); // ThrowStatement
}

These are the definitions of expression, statement and block from the Oracle's tutorial.

Expression

An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value.

Statement

Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon (;).

Block

A block is a group of zero or more statements between balanced braces

return; is a not a variable or method invocation, it's a statement. Hence, it must be enclosed in curly bases to form a block of code.

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