执行do while语句时不会产生错误

时间:2015-07-06 09:27:42

标签: java

为什么在以下程序中没有产生输出。有人可以解释程序的逻辑。

public class Test {
    public static void main(String[] args) {
        int l=1;
        do while (l<1)
        System.out.print("l is" +l);
        while (l>1);
    }
}

2 个答案:

答案 0 :(得分:1)

没有生成输出,因为您的do while()会将while视为其正文的第一个声明。

你在做什么实际上是这样的:

do 
    while (l<1)
        System.out.print("l is" +l);
while (l>1);

由于这是正确的语法,因此不会产生错误。

答案 1 :(得分:-1)

您实际上已创建了两个循环。

第一个在这里:

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<div class="compass_container" id="compass">
  <a id="square-1" class="square" href=""></a>
  <a id="square-2" class="square" href=""></a>
  <a id="square-3" class="square" href=""></a>
  <a id="square-4" class="square" href=""></a>
  <span class="arrow"></span>
</div>
</body>
</html>

这是一个没有正文且没有语句的do-while循环,因此它绝对不会做任何事情。

第二个循环在这里:

do while (l<1)

由于这个循环也没有正文,所以这个循环绝对不会做任何事情。

我相信您要寻找的格式如下:

while (l>1);

这是一个单循环,其主体包含一个语句(int l=1; do { System.out.print("l is" +l); } while (l<1); 调用)