首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Java示例中使用'For循环‘

在Java示例中使用'For循环‘
EN

Stack Overflow用户
提问于 2018-09-30 02:24:41
回答 1查看 163关注 0票数 2

我正在用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

上面的输出显示,当这首歌以一瓶啤酒结束时,它会说“墙上没有更多的啤酒”。

现在我的任务是以这个“啤酒之歌”为例,使用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循环时,输出未正确显示。有人能帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-30 02:28:12

问题是,在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");
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52571293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档