java未定义的标签outer,错误:未定义标签,如何在Java中的此代码中使用label语句?...

I read in textbooks for Java that any statement can be labeled and can be used with break.

But while trying this code i get error undefined label. (Guys at stackoverflow wait before marking this question as duplicate, i have checked those questions but none of those explain this problem).

public class LabelTest {

public static void main(String[] args) {

first: System.out.println("First statement");

for (int i = 0; i < 2; i++) {

System.out.println("Second statement");

break first;

}

}

}

解决方案

The scope of a label of a labeled statement is the immediately

contained Statement.

So in your case, the scope of lable first is the sysout statement following the lable. To be clearer, you can define the scope using curly braces, and within these braces its valid to jump to the label. So below are valid

first: {

System.out.println("First statement");

for (int i = 0; i < 2; i++) {

System.out.println("Second statement");

break first;

}

}

OR

first: {

System.out.println("First statement");

break first;

}

second:

for(int i=0;i<2;i++){

System.out.println("Second statement");

break second;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值