99 bottles of beer csharp example

esse quam videri
Revision as of 03:23, 9 February 2016 by Jeff (talk | contribs) (Text replacement - "syntaxhighlight lang="csharp" line="1" " to "syntaxhighlight lang="csharp"")
Jump to: navigation, search

99 bottles of beer lyrics for reference just in case you don't know how the song goes

Simple count down with while loop

99Bottles_whileSimple.cs

The simple version uses three variables

string strBottles = "bottles";
string strLineOne = "";
int intBottles = 99;

The string strBottles is used to change the word 'bottles' to bottle when the bottle count gets to 1.

2 bottles of beer on the wall.  is correct but
1 bottles of beer on the wall is not so we will use a variable and an if stement to change the
word bottles to bottle if the intBottles variable = 1
//Check for 1 bottle of beer
if (intBottles==1) 
{
   strBottles = " bottle"; //fix 1 bottle of beer
}

Now we can create the first line of the verse and write it to the console

strLineOne = intBottles + " " + strBottles + " of beer on the wall, " + intBottles + " bottles of beer.";

Console.WriteLine (strLineOne);

Now we just need to wrap this is a loop and make it count down. the loop may look like

while (intBottles > 0)
        {
            //Check for 1 bottle of beer
            if (intBottles==1) 
            {
                strBottles = " bottle"; //fix 1 bottle of beer
            }

            strLineOne = intBottles + " " + strBottles + " of beer on the wall, " + intBottles + " bottles of beer.";

            Console.WriteLine (strLineOne);
            
            intBottles--;
        }

the one we made in class

using System;
using System.Collections.Generic;
using System.Text;

namespace _9BottlesWhile
{
    class Program
    {
        static void Main(string[] args)
        {

            int intBottles = 99;
            string strLineOne = "";
            string strLineTwo = "";

            string strBottles = "bottles";

            while (intBottles >= 0)
            {
                if (intBottles == 0)
                {
                    strLineOne = "No more bottles of beer on the wall. No more bottles of beer.";
                    strLineTwo = "Got to the store and buy some more.";

                    Console.WriteLine(strLineOne);
                    Console.WriteLine(strLineTwo);

                    Console.ReadKey();
                    
                    intBottles = 99;  //this line will make it loop forever.
                }
                //Change bottles to bottle when on 1 bottle is left
                if (intBottles == 1)
                {
                    strBottles = "bottle";
                }
                
                strLineOne = intBottles + " " + strBottles + " of beer on the wall, ";
                strLineOne += intBottles + " " + strBottles + " of beer.";

                intBottles--;

                //Change bottles to bottle when on 1 bottle is left
                if (intBottles == 1)
                {
                    strBottles = "bottle";
                }
                if (intBottles == 0)
                {
                    strBottles = "bottles";
                }

                strLineTwo = "Take one down and pass it around. " + (intBottles); 
                strLineTwo += " " + strBottles + " of beer on the wall";


                Console.WriteLine(strLineOne);
                Console.WriteLine(strLineTwo);
                
            }

            Console.ReadKey();

        }
    }
}

Correct lyrics

The song sung the correct way

99Bottles_whileFull.cs

More Examples

There are many examples of 99 bottles of beer on

http://99-bottles-of-beer.net/

Their c# version is quite involved and possibly over cooked good luck understanding it