Difference between revisions of "99 bottles of beer csharp example"
esse quam videri
(removed dead links) |
|||
(9 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | [[ | + | [[Category:Programming]] |
+ | [[Category:Object Oriented Programming]] | ||
+ | [[Category:Programming Language Concepts]] | ||
− | |||
− | [ | + | [[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 | The simple version uses three variables | ||
− | <csharp> | + | <syntaxhighlight lang="csharp"> |
string strBottles = "bottles"; | string strBottles = "bottles"; | ||
string strLineOne = ""; | string strLineOne = ""; | ||
int intBottles = 99; | int intBottles = 99; | ||
− | </ | + | </syntaxhighlight> |
The string strBottles is used to change the word 'bottles' to bottle when the bottle count gets to 1. | The string strBottles is used to change the word 'bottles' to bottle when the bottle count gets to 1. | ||
Line 18: | Line 22: | ||
word bottles to bottle if the intBottles variable = 1 | word bottles to bottle if the intBottles variable = 1 | ||
− | <csharp> | + | <syntaxhighlight lang="csharp"> |
//Check for 1 bottle of beer | //Check for 1 bottle of beer | ||
if (intBottles==1) | if (intBottles==1) | ||
Line 24: | Line 28: | ||
strBottles = " bottle"; //fix 1 bottle of beer | strBottles = " bottle"; //fix 1 bottle of beer | ||
} | } | ||
− | </ | + | </syntaxhighlight> |
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"> |
strLineOne = intBottles + " " + strBottles + " of beer on the wall, " + intBottles + " bottles of beer."; | strLineOne = intBottles + " " + strBottles + " of beer on the wall, " + intBottles + " bottles of beer."; | ||
Console.WriteLine (strLineOne); | Console.WriteLine (strLineOne); | ||
− | </ | + | </syntaxhighlight> |
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"> |
while (intBottles > 0) | while (intBottles > 0) | ||
{ | { | ||
Line 49: | Line 53: | ||
intBottles--; | intBottles--; | ||
} | } | ||
− | </csharp> | + | </syntaxhighlight> |
+ | |||
+ | the one we made in class | ||
+ | <syntaxhighlight lang="csharp"> | ||
+ | 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(); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==Correct lyrics== | ||
The song sung the correct way | The song sung the correct way | ||
− | |||
− | |||
==More Examples== | ==More Examples== | ||
Line 61: | Line 135: | ||
http://99-bottles-of-beer.net/ | http://99-bottles-of-beer.net/ | ||
− | + | Their [http://99-bottles-of-beer.net/language-csharp-107.html c# version] is quite involved and possibly over cooked - good luck understanding it : ) |
Latest revision as of 15:27, 11 June 2019
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 : )