gpt4 book ai didi

java - Break 语句突然完成还是正常完成?

转载 作者:行者123 更新时间:2023-11-30 03:43:21 24 4
gpt4 key购买 nike

JLS 14.15:

A break statement with no label attempts to transfer control to the innermost enclosing switch, while, do, or for statement of the immediately enclosing method or initializer;

我对该规则有误解。例如,如果我们编写代码:

public static void main(String[] args) // Immediately enclosing method
{
while(true){ // Innermost enclosing 'while' for the break at LINE 2?
break; // **LINE 2**
}
}

JLS 说,当我们在 LINE 2 时,我们尝试将控制权转移到最内层的 while。做什么的?在 LINE 2 处,控制已经位于 while 的 block-statement 处。

最佳答案

您需要检查您引用的整个段落(为了清楚起见,我突出显示了粗体中的句子):

A break statement with no label attempts to transfer control to the innermost enclosing switch, while, do, or for statement of the immediately enclosing method or initializer; this statement, which is called the break target, then immediately completes normally.

在您的示例中,控制权转移到 while 语句以便终止并继续执行。

关于java - Break 语句突然完成还是正常完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26325668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com