我有一个 while 循环未执行。我不相信存在无限循环或启动问题,但是存在吗?我找不到我的逻辑错误!
public class Test
{
public static void main (String [] args)
{
int i = 1;
int j = 1;
while ((i < 10) && (j*j != 25));
{
i++;
++j;
System.out.println( i * j );
}
}
}
最佳答案
去掉while循环后面的分号
public static void main (String [] args)
{
int i = 1;
int j = 1;
while ((i < 10) && (j*j != 25)) //Semicolon removed from here
{
i++;
++j;
System.out.println( i * j );
}
}
如果没有用大括号括起来,直接在循环声明之后的任何语句都将被视为整个 block 。
IE
if( true )
System.out.println( "hello" );
System.out.println( "world" );
被视为
if( true ) {
System.out.println( "hello" );
}
System.out.println( "world" );
单个分号被视为 empty statement从而组成了整个循环体。
关于Java while循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13406522/