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: |
/// 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
| Version | Author | Date | Comments | Rate |
|---|---|---|---|---|
| 1 | Paul M. Parks | 04/20/05 | 8 | |
| 2 | Mark Hurley | 05/31/05 | 0 | |
| Shows new features of C# 2.0. | Bradley Tetzlaff | 11/24/05 | 0 |
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!
Comments
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.
(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.
Console.ReadKey(true);
to
Console.ReadLine();
Cool proggy
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);