public class BeerSong {
public static void main(String[] args) {
int beerNum = 99;
String word = "bottles";
while (beerNum > 0) {
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 == 1) {
word = "bottle"; // singular, as in ONE bottle.
}
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 else
} // end while loop
} // end main method
} // end class
这段代码打印出:如果while循环中
99 bottles of beer on the wall
99 bottles of beer on the wall.
99 bottles of beer.
Take one down and pass it around.
为什么不将其打印这样的:
99 bottles of beer on the wall
99 bottles of beer.
Take one down and pass it around, 99 bottles of beer on the wall.
由于if语句是在循环后
“这段代码打印出:”不,不。它会打印出'放下'和'将它们放在一起',不带''和'',并且使用不同的大小写和标点符号。 –
循环后面的'if'语句_isn't_。它在循环内部。 – khelwood
你的代码有什么问题?这看起来是正确的,尽管它不像你想要的那样打印,正如Andy Turner已经指出的那样。改变你的第三和第四个打印语句('System.out.println(“Take one down。”);''和'System.out.println(“Pass it around。”);')to'System.out.print “拿下一个并传递给它,”);'你应该几乎完成。 – Thomas