public static void main (String[] args) throws java.lang.Exception { int i = 0; d: { System.out.println("d"); } while(i < 10){ i++; continue d; } }
– 和 –
public static void main (String[] args) throws java.lang.Exception { int i = 0; d: { System.out.println("d"); while(i < 10){ i++; continue d; } } }
但以下工作正常:
public static void main (String[] args) throws java.lang.Exception { int i = 0; d: while(i < 10){ { System.out.println("d"); } i++; continue d; } }
它是否允许将控制转移到while,for或do语句?它没有在JLS中说.实际上说的是:
A continue statement with label Identifier attempts to transfer
control to the enclosing labeled statement (§14.7) that has the same
Identifier as its label; that statement,which is called the continue
target,then immediately ends the current iteration and begins a new
one.
解决方法
Does it allow to tranfer control to the only while,for or do statement? It doesn’t say in the JLS.
实际上,确实如此.
这是JLS 14.6(Java 8修订版)真正说的:
“A continue statement with label Identifier attempts to transfer control to the enclosing labeled statement (§14.7) that has the same Identifier as its label; that statement,which is called the continue target,then immediately ends the current iteration and begins a new one.
To be precise,a continue statement with label Identifier always completes abruptly,the reason being a continue with label Identifier.
The continue target must be a while,do,or for statement,or a compile-time error occurs.“
(用原文加粗!)
粗体句子说标签所附的声明(称为“继续目标”)必须是某种循环声明.
原文地址:https://www.jb51.cc/java/120940.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。