Java is the future !: 99 bottles of beer

Saturday, July 24, 2010

99 bottles of beer

Again a universal program for looping.

About:

"99 Bottles of Beer" is a traditional song in the United States and Canada. It is popular to sing on long trips, as it has a very repetitive format which is easy to memorize, and can take a long time to sing. In particular the song is frequently sung by children on long bus trips, such as class field trips, or on Scout and/or Girl Guide outings. The song is derived from the English "Ten Green Bottles".

The lyrics are simple:

Ninety-nine bottles of beer on the wall, Ninety-nine bottles of beer.
Take one down, pass it around, Ninety-eight bottles of beer on the wall.
The same verse is repeated, each time with one less bottle. The song is completed when the singer or singers reach zero.

So I made the program for this.

Source code:

public class HelloWorld{
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("No more bottles of beer on the wall");
}
}
}

No comments:

Post a Comment