Difference between revisions of "99 bottles of beer csharp example"

esse quam videri
Jump to: navigation, search
Line 3: Line 3:
 
Simple count down with while loop
 
Simple count down with while loop
  
[[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class3/99Bottles/99Bottles_whileSimple.cs]]
+
[[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class3/99Bottles/99Bottles_whileSimple.cs 99Bottles_whileSimple.cs]]
  
 
The simple version uses three variables
 
The simple version uses three variables
Line 19: Line 19:
  
 
<csharp>
 
<csharp>
 +
//Check for 1 bottle of beer
 +
if (intBottles==1)
 +
{
 +
  strBottles = " bottle"; //fix 1 bottle of beer
 +
}
 +
</csharp>
 +
 +
Now we can create the first line of the verse and write it to the console
 +
<csharp>
 +
strLineOne = intBottles + " " + strBottles + " of beer on the wall, " + intBottles + " bottles of beer.";
 +
 +
Console.WriteLine (strLineOne);
 +
</csharp>
 +
 +
Now we just need to wrap this is a loop and make it count down. the loop may look like
 +
<csharp>
 +
while (intBottles > 0)
 +
        {
 
             //Check for 1 bottle of beer
 
             //Check for 1 bottle of beer
 
             if (intBottles==1)  
 
             if (intBottles==1)  
Line 24: Line 42:
 
                 strBottles = " bottle"; //fix 1 bottle of beer
 
                 strBottles = " bottle"; //fix 1 bottle of beer
 
             }
 
             }
 +
 +
            strLineOne = intBottles + " " + strBottles + " of beer on the wall, " + intBottles + " bottles of beer.";
 +
 +
            Console.WriteLine (strLineOne);
 +
           
 +
            intBottles--;
 +
        }
 
</csharp>
 
</csharp>
  
 
The song sung the correct way
 
The song sung the correct way
  
http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class3/99Bottles/99Bottles_whileFull.cs
+
[[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class3/99Bottles/99Bottles_whileFull.cs 99Bottles_whileFull.cs]]

Revision as of 07:48, 17 September 2007

99 bottles of beer lyrics

Simple count down with while loop

[99Bottles_whileSimple.cs]

The simple version uses three variables <csharp> string strBottles = "bottles"; string strLineOne = ""; int intBottles = 99; </csharp>

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

<csharp> //Check for 1 bottle of beer if (intBottles==1) {

  strBottles = " bottle"; //fix 1 bottle of beer

} </csharp>

Now we can create the first line of the verse and write it to the console <csharp> strLineOne = intBottles + " " + strBottles + " of beer on the wall, " + intBottles + " bottles of beer.";

Console.WriteLine (strLineOne); </csharp>

Now we just need to wrap this is a loop and make it count down. the loop may look like <csharp> 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--;
       }

</csharp>

The song sung the correct way

[99Bottles_whileFull.cs]