Nit*_*tin 2 java compiler-errors
我在程序的下一行收到编译时"无法访问的代码"错误:
System.out.println("i ="+ i +",j ="+ j);
public static void main(String[] args) {
int i = 0, j = 5;
tp: for (;;)
{
i++;
for (;;)
{
if (i > --j) {
break tp;
}
}
System.out.println("i =" + i + ", j = " + j);
}
}
Run Code Online (Sandbox Code Playgroud)
请帮助我找出确切的原因.提前致谢.
让我们分析一下这段代码:
tp: for (;;) //<-- similar to while(true)
{
i++; //increases i by 1
for (;;) //<-- similar to while(true)
{
if (i > --j) { //decreases j and compares its value against i
break tp; //breaks tp, which means breaking the outer for loop
}
}
//while(true) above
//if break gets executed, it breaks this for loop
//so this line can never be executed
System.out.println("i =" + i + ", j = " + j);
}
Run Code Online (Sandbox Code Playgroud)
最简单的解决方案
移动 System.out.println("i =" + i + ", j = " + j);
到外for
循环外面.
tp: for (;;)
{
i++;
for (;;)
{
if (i > --j) {
break tp;
}
}
}
System.out.println("i =" + i + ", j = " + j);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
171 次 |
最近记录: |