Я очень новичок в программировании JAVA и раньше программировал на Python. Я пытаюсь понять, почему я получаю дубликат строки «Количество бутылок пива на стене», когда я выполняю свой код.Попытка узнать JAVA, и я хочу написать 99 бутылок пивной песни
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
Спасибо за вашу помощь
Конечно! почему я этого не увидел ... спасибо вам, – Snarre
Вам нужно будет переместить блок 'if', который решит« бутылку »или« бутылки »сразу после того, как вы вычтете один из« beerNum », чтобы избежать« 1 » бутылки пива на стене' (множественные «бутылки» на 1). – rgettman
Yupp получил! thnx – Snarre