在Java示例中使用“ For循环”

时间:2018-09-29 18:24:41

标签: java eclipse for-loop while-loop

我正在用Java进行练习。这是在for循环上使用的。下面的代码显示while循环。这是“啤酒之歌”的例子。

int beerNum = 99;
String word = "bottles";
while (beerNum > 0) {
   if (beerNum == 1) {
       word = "bottle";
   }
   System.out.println(beerNum + " " + word + " of beer on the wall");
   System.out.println(beerNum + " " + word + " of beer");
   System.out.println("Take one down.");
   System.out.println("Pass it around.");
   beerNum = beerNum -1;
   if (beerNum > 0) {
      System.out.println(beerNum + " " + word + " of beer on the wall");
   }
   else {
      System.out.println("No more bottles of beer on the wall");
   }
} // end loop

输出为:

OUTPUT:
-------------
99 bottles of beer on the wall
99 bottles of beer
Take one down.
Pass it around.
98 bottles of beer on the wall
98 bottles of beer on the wall
98 bottles of beer
Take one down.
Pass it around.
97 bottles of beer on the wall
97 bottles of beer on the wall
97 bottles of beer
Take one down.
Pass it around.
----------
----------
---------
---------

2 bottles of beer on the wall
2 bottles of beer on the wall
2 bottles of beer
Take one down.
Pass it around.
1 bottles of beer on the wall
1 bottle of beer on the wall
1 bottle of beer
Take one down.
Pass it around.
No more bottles of beer on the wall

上面的输出显示,当歌曲以1瓶啤酒结束时,它将显示“墙上不再有瓶啤酒”。

现在,我的任务是以“啤酒之歌”为例,并使用for循环而不是while循环来重写它。

我这样做了,但是输出看上去与while循环的输出不匹配。 这是使用for循环的代码和输出。

 String word = "bottles";
 for(int beerNum = 99; beerNum > 0; beerNum --) {
     if(beerNum==1) {
         word = "bottle";
     }
     System.out.println(beerNum + " " + word + " of beer on the wall");
     System.out.println(beerNum + " " + word + " of beer");
     System.out.println("Take one down.");
     System.out.println("Pass it around.");
     beerNum = beerNum -1;
     if (beerNum > 0) {
        System.out.println(beerNum + " " + word + " of beer on the wall");
     }
     else {
        System.out.println("No more bottles of beer on the wall");
     }

--------------------------------------------------------------------------
Output

99 bottles of beer on the wall
99 bottles of beer
Take one down.
Pass it around.
98 bottles of beer on the wall
97 bottles of beer on the wall (should be 98)
97 bottles of beer   (should be 98)
Take one down.
Pass it around.
96 bottles of beer on the wall 
95 bottles of beer on the wall (should be 96)
95 bottles of beer (should be 96)
Take one down.
Pass it around.
94 bottles of beer on the wall 
93 bottles of beer on the wall (should be 94)
93 bottles of beer (should be 94)
Take one down.
Pass it around.
--------------
--------------
--------------
    4 bottles of beer on the wall
3 bottles of beer on the wall (should be 4)
3 bottles of beer (should be 4)
Take one down.
Pass it around.
2 bottles of beer on the wall
1 bottle of beer on the wall (should be 2)
1 bottle of beer  (should be 2)
Take one down.
Pass it around.
No more bottles of beer on the wall

使用for循环,输出未正确显示。有人可以帮我吗?

2 个答案:

答案 0 :(得分:2)

问题是在for循环中,变量已被递减。 (for(int beerNum = 99; beerNum > 0; beerNum --)强调beerNum--

因此,语句beerNum = beerNum -1;将减少两次。您需要删除此行。

String word = "bottles";
for(int beerNum = 99; beerNum > 0; beerNum --) {
     if(beerNum==1) {
         word = "bottle";
    }
    System.out.println(beerNum + " " + word + " of beer on the wall");
    System.out.println(beerNum + " " + word + " of beer");
    System.out.println("Take one down.");
    System.out.println("Pass it around.");
    if (beerNum > 1) {
       System.out.println(beerNum -1 + " " + word + " of beer on the wall");
    } else {
       System.out.println("No more bottles of beer on the wall");
    }
}

答案 1 :(得分:1)

您正在将for循环代码中的 beerNum 变量减小两次。首先在循环标题中,然后在循环正文中。

这就是全部。