【问题标题】:Using 'For Loop' in Java Example在 Java 示例中使用“For 循环”
【发布时间】:2019-03-05 09:26:42
【问题描述】:

我正在用 Java 做一个练习。这是在for Loop 上使用的。下面的代码显示了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 循环时输出显示不正确。谁能帮我解决这个问题?

【问题讨论】:

  • 检查你在哪里减少变量。您可能会发现它被执行了两次。

标签: java eclipse for-loop while-loop


【解决方案1】:

问题是在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");
    }
}

【讨论】:

  • 我删除了 'beerNum = beerNum -1;'。输出完全符合我的预期。但它缺少一些东西。当歌曲以 1 瓶啤酒结束时,它不会显示“墙上不再有瓶啤酒”。
  • @AzizBokhari 这是因为beerNum 在循环结束时递减。要解决此问题,您只需执行 if(beerNum > 1) {//pint how many bottles}
  • 在第二个 if 中,它应该是 (beerNum-1),而不是 beerNum,以使其“看起来”它已被递减(并且单数化应该在那里发生)。
  • 感谢你们的帮助。得到正确的输出。
【解决方案2】:

您正在减少 for 循环代码中的 beerNum 变量 两次。首先是循环头,然后是循环体。

这就是全部。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    相关资源
    最近更新 更多