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#

(Concise, using C# 3.5 features)

Date:10/26/07
Author:Jeff Dietrich
URL:http://www.discordant.org
Comments:9
Info:n/a
Score: (3.20 in 25 votes)
/// A short and sweet C# 3.5 / LINQ implementation of 99 Bottles of Beer
/// Jeff Dietrich, jd@discordant.org - October 26, 2007

using System;
using System.Linq;
using System.Text;

namespace NinetyNineBottles
{
  class Beer
  {
    static void Main(string[] args)
    {
        StringBuilder beerLyric = new StringBuilder();
        string nl = System.Environment.NewLine;

        var beers =
            (from n in Enumerable.Range(0, 100)
             select new { 
               Say =  n == 0 ? "No more bottles" : 
                     (n == 1 ? "1 bottle" : n.ToString() + " bottles"),
               Next = n == 1 ? "no more bottles" : 
                     (n == 0 ? "99 bottles" : 
                     (n == 2 ? "1 bottle" : n.ToString() + " bottles")),
               Action = n == 0 ? "Go to the store and buy some more" : 
                                 "Take one down and pass it around"
             }).Reverse();

        foreach (var beer in beers)
        {
            beerLyric.AppendFormat("{0} of beer on the wall, {1} of beer.{2}",
                                    beer.Say, beer.Say.ToLower(), nl);
            beerLyric.AppendFormat("{0}, {1} of beer on the wall.{2}", 
                                    beer.Action, beer.Next, nl);
            beerLyric.AppendLine();
        }
        Console.WriteLine(beerLyric.ToString());
        Console.ReadLine();
    }
  }
}

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
1Paul M. Parks04/20/057
2Mark Hurley05/31/050
Shows new features of C# 2.0.Bradley Tetzlaff11/24/050

Comments

>>  Zain said on 01/27/08 21:51:58

Zain There are 16 errors in the program :o

>>  Marcelo said on 02/09/08 01:41:27

Marcelo Your programm starts at 98 should start at 99 =)

>>  Jeff Dietrich said on 03/06/08 20:02:51

Jeff Dietrich There are not 16 errors in the program - use .NET 3.5 *final*, build as a console app.

It doesn't start at 98 - the default command window doesn't have a buffer large enough to show 99. You'll see 98 first only if you don't adjust your buffer height beyond 300 rows.

>>  [ICR] said on 04/07/08 13:41:44

[ICR] The line:
(n == 2 ? "1 bottle" : n.ToString() + " bottles";)),
should read:
(n == 2 ? "1 bottle" : (n - 1).ToString() + " bottles";)),

It's also probably benificial to move the .Reverse() up to the Enumerable such that it reads:
(from n in Enumerable.Range(0, 100).Reverse()
as reversing the integers seems cheaper than reversing the objects. Though that's just intuition and I could be wrong.

>>  [ICR] said on 04/07/08 13:42:49

[ICR] Also, I tend to prefer using:
Console.ReadKey(true);
to
Console.ReadLine();

>>  DC said on 06/05/08 16:57:51

DC No one Cares what you prefer to use. Dumbass.

>>  Jack D said on 06/18/08 14:15:26

Jack D That is absolutely totally fantastic.
Cool proggy :)

>>  Krzysiuyo said on 06/23/08 17:13:59

Krzysiuyo StringBuilder beerLyric = new StringBuilder();

var beers =
(from n in Enumerable.Range(0, 100)
select new
{
Say = n == 0 ? "No more bottles" :
(n == 1 ? "1 bottle" : n.ToString() + " bottles";),
Next = n == 1 ? "no more bottles" :
(n == 0 ? "99 bottles" :
(n == 2 ? "1 bottle" : (n-1).ToString() + " bottles";)),
Action = n == 0 ? "Go to the store and buy some more" :
"Take one down and pass it around"
}).Reverse();

foreach (var beer in beers)
{
beerLyric.AppendFormat("{0} of beer on the wall.\n{1}, {2} of beer on the wall.\n\n",
beer.Say, beer.Action, beer.Next);
}

Console.WriteLine(beerLyric.ToString());
Console.ReadKey(true);

>>  anon said on 06/27/08 03:05:31

anon That wood B newlength << oldlength in C.

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: