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