&“未定义标签&"使用标记的中断时java中的错误 [英] "Undefined label" error in java when using labeled break
问题描述
我正在尝试分析Java代码并查看其功能,但是在中断标签"行中给出了未定义标签"错误.据我了解,与c#不同,java可以突破当前范围之外的范围.是不是?
I'm trying to analyse a java code and see what it does, but in the "break label" line it gives the "Undefined label" error. from my understanding java unlike c# allows breaking into a scope outside of the current scope. isn't that right?
if (conditions) {
// some code
if (conditions) {
break label;
}
// some code
}
for (;;) {
if (conditions) {
// some code
}
// some code
break;
label:
// some code
}
推荐答案
在您的示例中,您尝试使用 break标签;
就像它与 goto标签一样;
C/C ++中的语法.这不是Java中的工作方式.
In your example, you are trying to use break label;
as though it were similar to the goto label;
syntax in C/C++. This is not how it works in Java.
在Java中, break标签;
语法仅在使用嵌套循环并且需要从一个内部循环扩展到所讨论的外部循环之一时适用.
in Java, the break label;
syntax is applicable only when you are using nested loops and you need to break out from an inner loop to one of the outer loops in question.
例如:
outer: while(some condition) {
inner: while(another condition) {
innermost: while(yet another condition) {
if(breaking_condition) {
break inner;
} else if (one more breaking condition) {
break outer;
}
}
}
}
从Oracle参考本教程:
Refer this tutorial from Oracle:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
希望这会有所帮助!
这篇关于&“未定义标签&"使用标记的中断时java中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!