News:

Please note these forums are mostly a testing ground for my SMF work and I don't really use them otherwise.

Main Menu

Paste-1219203724:v:use_geshi-1:v:type-java

Started by SleePy, Aug 20, 2008, 03:42 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

SleePy

import java.util.Scanner;

public class BeerSong
{
  public static void main(String[] args)
  {
    Scanner keyboard = new Scanner(System.in);
    int beers = 99;

    try
    {
      System.out.println("How many beers are on the wall (limit of 99)?");
      beers = keyboard.nextInt();
    }
    catch (RuntimeException e)
    {
      System.out.println("ERROR: Invalid Number input, defaulting to 99");
    }

    if (beers > 99)
      beers = 99;
    else if (beers < 0)
      beers = 0;
   
    printSong(beers);
  }
 
  public static void printSong(int beers)
  {
    for (int i = beers; i >= 0; i--)
    {    
      if (i == 0)
      {
        System.out.println("No bottles of beer on the wall, no bottles of beer.");
        System.out.print("Go to the store and buy some more");
       
        if (beers != 0)
          System.out.print(", " + beers);
        System.out.println(" bottles of beer on the wall");
      }
      else
      {
        String alphaBeers = ConvertToAlpha(i);
        String alphaNewBeers = ConvertToAlpha((i - 1));

        System.out.println(alphaBeers + " bottles of beer on the wall, " + alphaBeers + " bottles of beer.");
        System.out.println("Take one down and pass it around, " + alphaNewBeers + " beers on the wall.");
        System.out.println("");
      }
    }
  }
 
  private static String ConvertToAlpha(int number)
  {
    if (number < 14)
      return intToAlpha(number);
    else
    {
      String temp = "";

      if ((number % 10) != 0)
        temp = (number > 20 ? "-" : "") + ConvertToAlpha((number % 10));

      if (number < 20)
        return temp + TenIntToAlpha((number / 10));
      return TenIntToAlpha((number / 10)) + temp;
    }
  }
 
  private static String intToAlpha(int number)
  {
    String[] alphaNumbers = new String[] {"No More", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen"};
    return alphaNumbers[number];
  }
 
  private static String TenIntToAlpha(int number)
  {
    String[] alphaNumbers = new String[] {"", "teen", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    return alphaNumbers[number];
  }
}
No siggy! :D