Java while循环问题

标签 java while-loop

我有一个 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/

相关文章:

java - 知道任何可嵌入的 NNTP 服务器吗?

java - 编写数值线性代数编译器

php - mysql_fetch_assoc() 仅适用于 WHILE 循环中的一次迭代

java - 如何通过输入将尽可能多的元素添加到数组列表中?

bash - 重定向 bash while 循环中的变量丢失

java - Apache/Tomcat服务器中显示汉字

Java Jackcess 库文档?

java - Python 中的 Tensorflow Java Api `toGraphDef` 等价物是什么?

java - while循环/switch语句

python - 我可以在 while 循环内以输出形式显示数据框吗?