Friday, January 9, 2015

Java - 99 Bottles of Beer on the Wall *FIX*

In the Heads-On Java activity "Coding a Serious Business Application", it showed us a finished program that runs through the song "99 Bottles of Beer on the Wall", but with a small problem that we needed to find and fix. 

The problem ended up being an if statement put before the rest of the content within the while loop, that needed to be integrated farther down. The if statement regarded beerNum equaling 1, changing the word from "bottles" to "bottle" so it made sense with a quantity of 1. It was originally before the "beerNum = beerNum - 1" statement, which decreases the amount of bottles. It needed to be after it, so the program can check for the bottle equaling 1 before it says the next phrase. I commented where the if statement used to be, and highlighted where I put it to fix the program.

public class BeerSong {

    public static void main(String[] args) {

        int beerNum = 99;
        String word = "bottles";
        
        while (beerNum > 0) {

            //This is where the if statment used to be
            
            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");
            } else {
                System.out.println("No more bottles of beer on the wall");
            }
        }
    }  
}

No comments:

Post a Comment