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文は、ループの後にあるので
「このコードは、プリントアウトし:」いいえ、それはしません。 'と'を使わず、大文字と句読点を変えずに、 'Take down down'と' Pass it around 'を別々の行に出力します。 –
'if'ステートメントはループの後にはありません。それはループ内にあります。 – khelwood
あなたのコードにはどのような問題がありますか? Andy Turnerがすでに指摘しているように、あなたが望むように印刷されませんが、正しいと思われます。 3行目と4行目のprint文を変更する( 'System.out.println(" Take down down。 ");'と 'System.out.println("それを渡す。 ");') 'System.out.print( "1つを取って周りを渡す");そしてあなたはほとんど完了するべきです。 – Thomas