Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to code the 99 bottles of beer song

Tags:

java

public class apples {
    public static void main(String[] args) {
        int beerNum = 99;
        String word = "bottles";

        while (beerNum > 0) {

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

            System.out.println(beerNum + " " + word + " of beer on the wall, " + beerNum + " " + word + " of beer");
            beerNum = beerNum - 1;

            if (beerNum > 0) {
                System.out.println("Take one down, pass it round " + beerNum + " " + word + " of beer");
            }
        }

        if (beerNum == 0) {
            System.out.println("No more bottles of beer");
        }

    }
}

The output is:

99 bottles of beer on the wall, 99 bottles of beer
Take one down, pass it round 98 bottles of beer
98 bottles of beer on the wall, 98 bottles of beer
Take one down, pass it round 97 bottles of beer
97 bottles of beer on the wall, 97 bottles of beer
Take one down, pass it round 96 bottles of beer
96 bottles of beer on the wall, 96 bottles of beer
Take one down, pass it round 95 bottles of beer
95 bottles of beer on the wall, 95 bottles of beer... 

(And so on and so forth)

3 bottles of beer on the wall, 3 bottles of beer
Take one down, pass it round 2 bottles of beer
2 bottles of beer on the wall, 2 bottles of beer
Take one down, pass it round 1 bottles of beer
1 bottle of beer on the wall, 1 bottle of beer
No more bottles of beer

Why isn't the String word equalling "bottle"? Instead it says "bottles" in "Take one down, pass it round 1 BOTTLES of beer.

Also after "1 bottle of beer on the wall, 1 bottle of beer" it doesn't say "Take one down pass it round"

Link to the lyrics.

like image 281
Stephen-Wisniewski Avatar asked Sep 15 '15 01:09

Stephen-Wisniewski


2 Answers

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.

like image 125
Sandeep Rout Avatar answered Nov 05 '22 13:11

Sandeep Rout


Try this it will work..

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");
            }
            else {
                System.out.println("no more bottles of beer on the wall");
            }
    }

}
like image 1
pranav kumar Avatar answered Nov 05 '22 14:11

pranav kumar