Trying to code the 99 bottles of beer song

Try this code:

public class BeerSong{
public static void main (String[] args){
    int beerNum = 99;
    String word = "bottles";
    while(beerNum > 0){
        if (beerNum == 1){
            word = "bottle";
        }
        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");
            System.out.println("***************************");
        }else {
            System.out.println("No more bottles of beer on the wall");
        }
    }
}
}

It will run with the 1 bottles of beer on the wall output. To correct this code 100%
Just move the if statement

beerNum = beerNum - 1;
        if (beerNum == 1){
            word = "bottle";
        }

after

beerNum = beerNum - 1;

Like this

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";
        }
        if (beerNum > 0){
            System.out.println(beerNum + " " + word + " of beer on the wall");
            System.out.println("***************************");
        }else {
            System.out.println("No more bottles of beer on the wall");
        }
    }
}
}

I use the System.out.println("************") because it will give a clear idea when one loop ends and other starts.

Tags:

Java