Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language C#

Date:05/31/05
Author:Mark Hurley
URL:n/a
Comments:0
Info:n/a
Score: (2.94 in 16 votes)
// Simplistic, yet working C# sample
// Author: Mark Hurley	(markph@mailcan.com)
// May 30, 2005

using System;

namespace NinetyNineBottlesOfBeer
{
	/// <summary>
	/// Infamous 99 bottles of beer song in C#.Net
	/// </summary>
	class NinetyNineBottlesOfBeerSong
	{
		/// <summary>
		/// beer verse more beer left
		/// </summary>
		private const string BEER_LYRICS_MORE = @"
{0} bottle{1} of beer on the wall,
{0} bottle{1} of beer.
Take one down, pass it around,
{2} bottle{3} of beer on the wall.";

		/// <summary>
		/// beer verse no more beer left
		/// </summary>
		private const string BEER_LYRICS_NONE = @"
{0} bottle{1} of beer on the wall,
{0} bottle{1} of beer.
Take one down, pass it around,
No more bottles of beer on the wall.";

		/// <summary>
		/// Determine the proper verse, then merge it with <c>count</c>.
		/// </summary>
		/// <param name="count">Number of bottles remaining.</param>
		/// <returns>Properly formated string verse for song.</returns>
		public string Sing(int count)
		{
			string tmp = "";
			if (count == 1)
				return string.Format(BEER_LYRICS_NONE, 
					count, 
					(count==1) ? "" : "s");
			else if (count > 0)
				return string.Format(BEER_LYRICS_MORE, 
					count, 
					(count==1) ? "" : "s",
					(count-1),
					((count-1)==1) ? "" : "s");
			else
				tmp = "";

			return tmp;
		}

		[STAThread]
		static void Main(string[] args)
		{
			NinetyNineBottlesOfBeerSong song = new NinetyNineBottlesOfBeerSong();

			for(int i=99; i>0; i--)
			{
				Console.WriteLine(song.Sing(i));
				Console.ReadLine();
			}
		}
	}
}

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Concise, using C# 3.5 featuresJeff Dietrich10/26/0714
2Paul M. Parks04/20/059
C# Gratuitously FunctionalPaul Stancer04/14/091
corporate styleveteran corporate coder08/16/090
Uses LinqSudipta11/19/100
Shows new features of C# 2.0.Bradley Tetzlaff11/24/050
v3.0 Easy Functional RecursiveYelinna Pulliti Carrasco08/04/090

Comments

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: