Java break的问题

来源:百度知道 编辑:UC知道 时间:2024/05/17 23:31:27
public class breakTest2
{
public static void main(String args[])
{
String result = "";
my:for(int i=1; i<6; i++)
{
for(int j=1; j<8; j++)
{
result = i+";"+j;
if(i==4)
{
break my;
}
}
}
System.out.println(result);
}
}

当break了外层循环时,里层循环被break了吗?
他这个输出的是 4;1

为什么不是4;1
4;2
4;3
4;4
4;5
4;6
4;7

???

因为你的System.out.println(result);写错地方了,如果想得到你的结果应该这样
public class breakTest2
{
public static void main(String args[])
{
String result = "";
my:for(int i=1; i<6; i++)
{
for(int j=1; j<8; j++)
{
result = i+";"+j;
if(i==4)
{
System.out.println(result);
break my;
}
}
}

}
}