Skip to content

Instantly share code, notes, and snippets.

@Kyle-Mendes
Last active August 29, 2015 14:20
Show Gist options
  • Save Kyle-Mendes/2e3a483f16317d8a652d to your computer and use it in GitHub Desktop.
Save Kyle-Mendes/2e3a483f16317d8a652d to your computer and use it in GitHub Desktop.
Bottles of Beer in C#
using System;
namespace Bottles_of_Beer
{
class Program
{
static void Main(string[] args)
{
CountBeers();
}
static void CountBeers()
{
Console.WriteLine("How many bottles of beer do you have?");
string bottles = Console.ReadLine();
int beers = 0; bool success = false;
try {
beers = Convert.ToInt32(bottles);
success = true;
}
catch (FormatException) {
Console.WriteLine("{0} is not a number. Maybe you've had enough. \n", bottles);
KickOutTheDrunk();
}
catch (OverflowException) {
Console.WriteLine("That's way too many beers! You should probably talk to someone. \n");
KickOutTheDrunk();
}
finally {
if (beers > 0) {
SingAboutBeer(beers);
} else if (beers <= 0 && success) {
Console.WriteLine("You need to have some beers for this to work. \n", beers);
KickOutTheDrunk();
}
}
}
static void SingAboutBeer(int beers)
{
while (beers > 0)
{
Console.WriteLine("{0} bottles of beer on the wall,", beers);
Console.WriteLine("{0} bottles of beer.", beers);
Console.WriteLine("You take one down, pass it around,");
beers--;
Console.WriteLine("{0} bottles of beer on the wall. \n", beers);
}
Console.WriteLine("Go again?! Y/N \n");
string go = Console.ReadLine();
if (go == "Y" || go == "y")
{
CountBeers();
}
else
{
Console.WriteLine("See you again soon! (Press any key to exit) \n");
Console.ReadKey();
}
}
static void KickOutTheDrunk()
{
Console.WriteLine("Come back when you've sobered up. (Press any key to exit) \n");
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment