java 在 if-else 语句中使用 label 和 break

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14835815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:33:34  来源:igfitidea点击:

Using label and break in if-else statement

javalabelbreak

提问by user2044187

I have this code which generates random number from an array, at a particular condition (when x and y both are equal to zero). I want the control to jump to the label. But the control never jumps to the label in any condition. I wanted to know that whether I am doing it right or not?

我有这段代码,它在特定条件下(当 x 和 y 都等于 0 时)从数组中生成随机数。我希望控件跳转到标签。但是控件在任何情况下都不会跳转到标签。我想知道我做得对还是不对?

int[] arr = {0, 1, 2};    
Random rn = new Random();
label: {
    //some code
    if (x != 0 && y !=0) {
        //some code
    } else {
        break label;
    }
}

回答by rath

Without examining whether you should, yes, you can use labeled statements with ifin Java. According to the 1.7 specification

无需检查是否应该,yes,您可以if在 Java 中使用带标签的语句。根据1.7规范

The Identifier is declared to be the label of the immediately contained Statement. [...] identifier statement labels are used with break(§14.15) or continue(§14.16) statements appearing anywhere within the labeled statement.

标识符被声明为立即包含的语句的标签。[...] 标识符语句标签与出现在标记语句中任何位置的break(第 14.15 节)或continue(第 14.16 节)语句一起使用。

It goes on (emphasis added)

继续(强调)

If the statement is labeled by an Identifier and the contained Statement completes abruptly because of a break with the same Identifier, then the labeled statement completes normally. In all other cases of abrupt completion of the Statement, the labeled statement completes abruptly for the same reason.

如果语句由标识符标记并且包含的​​语句由于具有相同标识符的中断而突然完成,则标记语句正常完成。在语句突然完成的所有其他情况下,标记语句出于相同原因突然完成。

So if you breakan ifblock (remember a block is a statement), you can exit the ifbody. Let's test it:

所以如果你break是一个if块(记住一个块是一个语句),你可以退出if主体。让我们测试一下:

public static void main(String[] args) {
    if (true) label: {
        if (args != null)
            break label;
        System.out.println("doesn't get here");
    }
    System.out.println("Outside of labeled block");
}

Output:

输出:

Outside of labeled block

回答by Brian Roach

The breakstatement breaks loopsand does not transfer control to the label.

break语句中断循环并且不会将控制转移到标签。

Using a label with breakis for when you have inner and outer loops. An unlabeled breakbreaks the inner most loop (the one you are in) whereas a labeled breakallows you to specify an outer loop.

break当您有内循环和外循环时,使用带有is的标签。未标记的break中断最内层循环(您所在的循环),而标记break允许您指定外部循环。

See: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

请参阅:http: //docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

Specifically the BreakWithLabelDemo

特别是BreakWithLabelDemo

回答by sp00m

Try toAvoid using labels. What you could do there, is:

尽量避免使用标签。你可以在那里做的是:

while(true) {
    if(x == 0 && y == 0) {
        break;
    }
    // some stuff with x and y
}

回答by Adeeb

I suggest that you use a recursive function.

我建议你使用递归函数。

public void work(){
    // some code
    if (x != 0 && y != 0) {
        //some code
    } else {
        work();
    }
} 

You cannot use breakwithout loops

你不能在break没有循环的情况下使用

回答by Miquel

As far as I know, labels cannot be used arbitrarily around {}, you need to do them to mark a for, while or do-while loop

据我所知,标签不能随意使用{},你需要用它们来标记 for、while 或 do-while 循环