I’ll Tell You What…

March 9, 2008

BeerSong.java

Filed under: Programming, Java — Larry @ 1:55 am

/**
 * Simple program that prints out the words
 * to the 99 Bottles of Beer on the Wall
 * song.
 *
 * Based on the BeerSong example in the
 * _Head_First_Java_ book with corrections.
 */
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.");

            if (beerNum == 1) {
                System.out.println("Take it down.");
                word = "bottle";
            } else {
                System.out.println("Take one down.");
            }

            System.out.println("Pass it around.");
            beerNum--;

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

            if (beerNum > 0) {
                System.out.println(beerNum + " " + word + " of beer on the wall.\n");
            } else {
                System.out.println("No more bottles of beer on the wall.");
            }
        }
    }
}

1 Comment »

  1. /**
    * Simple program that prints out the words
    * to the 99 Bottles of Beer on the Wall
    * song.
    *
    * Based on the BeerSong example in the
    * _Head_First_Java_ book with corrections.
    */
    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.”);

    if (beerNum != 1) {
    System.out.println(”Take one down.”);
    }
    else {
    System.out.println(”Take it down.”);
    }

    System.out.println(”Pass it around.”);
    beerNum–;

    switch (beerNum) {
    case 0:
    System.out.println(”No more bottles of beer on the wall.”);
    break;
    case 1:
    word = “bottle”;
    // yes, there is no break;
    default:
    System.out.println(beerNum + ” ” + word + ” of beer on the wall.\n”);
    break;
    }
    }
    }
    }

    Comment by Nek Strebor — April 7, 2008 @ 12:24 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

29 queries. 1.981 seconds. Powered by WordPress