I am very new to JAVA programming and have only programmed in Python before. I'm trying to figure out why I'm getting a duplicate line of " # of bottles of beer on the wall" when I execute my code.
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");
}
}
}
}
The result I get is this:
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
Thank you for your help
The last printed line of the loop is identical to the first printed line of the next loop, so it's not a problem. To visually separate each loop's output, print a blank line at the very end of the method. Then you'll see something like this:
2 bottles of beer on the wall // Last line of a loop
2 bottles of beer on the wall // First line of the next loop
2 bottles of beer
Take one down
pass it around
1 bottles of beer on the wall // Last line of a loop
1 bottle of beer on the wall // First line of next loop
1 bottle of beer
Take one down
pass it around
No more bottles of beer on the wall
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With