Difference between revisions of "99 bottles of beer csharp example"
esse quam videri
m (Text replacement - "</csharp>" to "</syntaxhighlight>") |
m (Text replacement - "syntaxhighlight lang="csharp" line="1" " to "syntaxhighlight lang="csharp"") |
||
Line 8: | Line 8: | ||
The simple version uses three variables | The simple version uses three variables | ||
− | <syntaxhighlight lang="csharp | + | <syntaxhighlight lang="csharp"> |
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 | ||
− | <syntaxhighlight lang="csharp | + | <syntaxhighlight lang="csharp"> |
//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 | ||
− | <syntaxhighlight lang="csharp | + | <syntaxhighlight lang="csharp"> |
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 | ||
− | <syntaxhighlight lang="csharp | + | <syntaxhighlight lang="csharp"> |
while (intBottles > 0) | while (intBottles > 0) | ||
{ | { | ||
Line 54: | Line 54: | ||
the one we made in class | the one we made in class | ||
− | <syntaxhighlight lang="csharp | + | <syntaxhighlight lang="csharp"> |
using System; | using System; | ||
using System.Collections.Generic; | using System.Collections.Generic; |
Revision as of 03:23, 9 February 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
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
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