Exploring more into some simple C# syntax. How about a program that greets people at Le Bar, we want to get their name and age and be able to inquire whether they want a beer if they’re at least 21, or, if they’re underage, a water or soda.
The first thing the program does is greet the visitor and then asks for their name. The user is able to input their name because the variable ‘yourName’ is defined as Console.ReadLine(), which means it will accept the input from the user and define the value yourName as whatever was entered – hopefully your name!
After the visitor supplies the name the program then prompts the user to enter their age. Again, the Console.ReadLine() comes in to allow the user to provide input. However, something fishy happens here. I’ve encased Console.ReadLine() with int.Parse(). Why? The ReadLine() accepts the user input as a STRING, not an INT
When the ReadLine() method is encapsulated by int.Parse however, the compiler will do it’s best to the string value into an integer thus allowing us to define yourAge as, well, your age.
So now that the visitors name and age is known, we have to provide the appropriate response to offer them either a beer or something a little less alcoholy. This is where an if statement comes in!
The program says if your age is greater than or equal to 21, display the text
So you’re old enough to drink, “+ yourName +“, Would you like a beer?
then uses the else statement to determine that anyone else must be underage and cannot drink, thus displaying
You’re not old enough to drink, “+ yourName +“, Would you like a water or soda?
More C# fun to come soon.
And also, to my fellow nerds and dorks, I highly recommend getting the Lootcrate (http://www.lootcrate.com/). I did, and I love it.