Difference between revisions of "99 bottles of beer csharp example"
esse quam videri
m (Text replacement - "<csharp>" to "<syntaxhighlight lang="csharp" line="1" >") |
m (Text replacement - "</csharp>" to "</syntaxhighlight>") |
||
Line 12: | Line 12: | ||
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 26: | Line 26: | ||
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 | ||
Line 33: | Line 33: | ||
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 | ||
Line 51: | Line 51: | ||
intBottles--; | intBottles--; | ||
} | } | ||
− | </ | + | </syntaxhighlight> |
the one we made in class | the one we made in class | ||
Line 121: | Line 121: | ||
} | } | ||
} | } | ||
− | </ | + | </syntaxhighlight> |
==Correct lyrics== | ==Correct lyrics== |
Revision as of 18:29, 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
1 string strBottles = "bottles";
2 string strLineOne = "";
3 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
1 //Check for 1 bottle of beer
2 if (intBottles==1)
3 {
4 strBottles = " bottle"; //fix 1 bottle of beer
5 }
Now we can create the first line of the verse and write it to the console
1 strLineOne = intBottles + " " + strBottles + " of beer on the wall, " + intBottles + " bottles of beer.";
2
3 Console.WriteLine (strLineOne);
Now we just need to wrap this is a loop and make it count down. the loop may look like
1 while (intBottles > 0)
2 {
3 //Check for 1 bottle of beer
4 if (intBottles==1)
5 {
6 strBottles = " bottle"; //fix 1 bottle of beer
7 }
8
9 strLineOne = intBottles + " " + strBottles + " of beer on the wall, " + intBottles + " bottles of beer.";
10
11 Console.WriteLine (strLineOne);
12
13 intBottles--;
14 }
the one we made in class
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace _9BottlesWhile
6 {
7 class Program
8 {
9 static void Main(string[] args)
10 {
11
12 int intBottles = 99;
13 string strLineOne = "";
14 string strLineTwo = "";
15
16 string strBottles = "bottles";
17
18 while (intBottles >= 0)
19 {
20 if (intBottles == 0)
21 {
22 strLineOne = "No more bottles of beer on the wall. No more bottles of beer.";
23 strLineTwo = "Got to the store and buy some more.";
24
25 Console.WriteLine(strLineOne);
26 Console.WriteLine(strLineTwo);
27
28 Console.ReadKey();
29
30 intBottles = 99; //this line will make it loop forever.
31 }
32 //Change bottles to bottle when on 1 bottle is left
33 if (intBottles == 1)
34 {
35 strBottles = "bottle";
36 }
37
38 strLineOne = intBottles + " " + strBottles + " of beer on the wall, ";
39 strLineOne += intBottles + " " + strBottles + " of beer.";
40
41 intBottles--;
42
43 //Change bottles to bottle when on 1 bottle is left
44 if (intBottles == 1)
45 {
46 strBottles = "bottle";
47 }
48 if (intBottles == 0)
49 {
50 strBottles = "bottles";
51 }
52
53 strLineTwo = "Take one down and pass it around. " + (intBottles);
54 strLineTwo += " " + strBottles + " of beer on the wall";
55
56
57 Console.WriteLine(strLineOne);
58 Console.WriteLine(strLineTwo);
59
60 }
61
62 Console.ReadKey();
63
64 }
65 }
66 }
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