我对 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
谢谢您的帮助