Difference between revisions of "99 bottles of beer csharp example"
m (Text replacement - "<csharp>" to "<syntaxhighlight lang="csharp" line="1" >") |
|||
Line 8: | Line 8: | ||
The simple version uses three variables | The simple version uses three variables | ||
− | <csharp> | + | <syntaxhighlight lang="csharp" line="1" > |
string strBottles = "bottles"; | string strBottles = "bottles"; | ||
string strLineOne = ""; | string strLineOne = ""; | ||
Line 20: | Line 20: | ||
word bottles to bottle if the intBottles variable = 1 | word bottles to bottle if the intBottles variable = 1 | ||
− | <csharp> | + | <syntaxhighlight lang="csharp" line="1" > |
//Check for 1 bottle of beer | //Check for 1 bottle of beer | ||
if (intBottles==1) | if (intBottles==1) | ||
Line 29: | Line 29: | ||
Now we can create the first line of the verse and write it to the console | Now we can create the first line of the verse and write it to the console | ||
− | <csharp> | + | <syntaxhighlight lang="csharp" line="1" > |
strLineOne = intBottles + " " + strBottles + " of beer on the wall, " + intBottles + " bottles of beer."; | strLineOne = intBottles + " " + strBottles + " of beer on the wall, " + intBottles + " bottles of beer."; | ||
Line 36: | Line 36: | ||
Now we just need to wrap this is a loop and make it count down. the loop may look like | Now we just need to wrap this is a loop and make it count down. the loop may look like | ||
− | <csharp> | + | <syntaxhighlight lang="csharp" line="1" > |
while (intBottles > 0) | while (intBottles > 0) | ||
{ | { | ||
Line 54: | Line 54: | ||
the one we made in class | the one we made in class | ||
− | <csharp> | + | <syntaxhighlight lang="csharp" line="1" > |
using System; | using System; | ||
using System.Collections.Generic; | using System.Collections.Generic; |
Revision as of 18:28, 25 January 2016
99 bottles of beer lyrics for reference just in case you don't know how the song goes
Simple count down with while loop
The simple version uses three variables <syntaxhighlight lang="csharp" line="1" > 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
<syntaxhighlight lang="csharp" line="1" > //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 <syntaxhighlight lang="csharp" line="1" > 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 <syntaxhighlight lang="csharp" line="1" > 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 one we made in class <syntaxhighlight lang="csharp" line="1" > 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();
} }
} </csharp>
Correct lyrics
The song sung the correct way
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