Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
努力学习JAVA,我想写99瓶啤酒歌_Java - Fatal编程技术网

努力学习JAVA,我想写99瓶啤酒歌

努力学习JAVA,我想写99瓶啤酒歌,java,Java,我对JAVA编程非常陌生,以前只使用Python编程。 我试图弄明白,为什么在执行代码时会出现“墙上有几瓶啤酒”的重复行 package BeerBottle; public class BeerBot { public static void main (String [] args){ int beerNum = 99; String word = "bottles"; while (beerNum > 0) {

我对JAVA编程非常陌生,以前只使用Python编程。 我试图弄明白,为什么在执行代码时会出现“墙上有几瓶啤酒”的重复行

    package BeerBottle;

    public class BeerBot {
  public static void main (String [] args){
      int beerNum = 99;
      String word = "bottles";

      while (beerNum > 0) {

      if (beerNum == 1) {
      word = "bottle";
      } else {
      word = "bottles";
      }
    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"); // I think it might be this line but I need it 
    } else {
        System.out.println("No more bottles of beer on the wall");
    }
    }
   }
}

我得到的结果是:

    2 bottles of beer on the wall
    2 bottles of beer on the wall (duplicate)
    2 bottles of beer
    Take one down
    pass it around
    1 bottles of beer on the wall
    1 bottle of beer on the wall (duplicate)
    1 bottle of beer
    Take one down
    pass it around
    No more bottles of beer on the wall

感谢您的帮助

循环的最后一行打印内容与下一个循环的第一行打印内容相同,因此这不是问题。要直观地分离每个循环的输出,请在方法的末尾打印一个空行。然后你会看到这样的情况:

2 bottles of beer on the wall  // Last line of a loop

2 bottles of beer on the wall  // First line of the next loop
2 bottles of beer
Take one down
pass it around
1 bottles of beer on the wall  // Last line of a loop

1 bottle of beer on the wall   // First line of next loop
1 bottle of beer
Take one down
pass it around
No more bottles of beer on the wall

你确定你真的得到了一个意外的重复行,并且没有把一节的最后一行和下一节的第一行弄错吗?尝试在每一节的末尾添加一个额外的空行,以清楚地看到差异

这可以通过额外的System.out.println(“”)或在上一个系统的末尾添加“\n”来完成


此外,当你减少啤酒的数量时,你会想重新评估你正在使用的啤酒词。

我会使用
for
循环,而不是
while
。我可以在最后两次迭代中退出循环,这必须处理“瓶子/瓶子”的东西

我的循环将如下所示:

for (int num_beers=99; i>2; i--) {
  String output = num_bers + " bottles of beer";

  System.out.println(output + " on the wall");
  System.out.println(output);
  System.out.println("Take one down\npass it around");
  System.out.println(output.replace(num_beers, num_beers-1) + " on the wall");
}
有了这个循环,最后打印的字符串将是“墙上有两瓶啤酒”(第一次)


仅用于实践,制作相同的示例是很好的,但使用递归方法

您需要在从
beerNum
中减去一瓶后立即将决定“瓶子”与“瓶子”的
if
块移动,以避免
1瓶啤酒挂在墙上
(1上的复数“瓶子”).最好把这个案子从循环中弄出来,因为如果它做的不完全一样,我们为什么要像它做的那样对待它?