More C#, Would you like a drink?

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.

06262014_Csharp_Drink(Hopefully this image isn’t for ants)

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

06262014_Csharp_Drink_StringReadLine

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?

END!

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.

Picture 2Picture 4 Picture 5Picture 9

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s