Illustrator Code-Along
Follow the instructions below to build a simple menu-based application that can draw some ASCII art.
Printing the Welcome Message
Start by printing out a message that says "Welcome to the Illustrator!"
After the initial Console.WriteLine
, add another empty Console.WriteLine
statement just to add a space beneath the first message. Run the program and ensure that the message appears in the console.
Code
using System;
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Welcome to the Illustrator!");
Console.WriteLine();
}
}
Printing the Menu
Next, print out the menu for the application. It should start with another empty Console.WriteLine
for some more space. It should then ask the user which command they would like to execute. To start, add one option for a command: "1. Draw Bunny"
. Add empty Console.WriteLine
statements to make spaces between the messages where necessary. Run the program and ensure that the menu text appears as expected.
Code
Console.WriteLine();
Console.WriteLine("What would you like to do?");
Console.WriteLine();
Console.WriteLine("1. Draw Bunny");
Console.WriteLine();
Receiving User Input
Once the user has been prompted, it is necessary to receive their input from the console.
- Create a new
string
variable namedcommand
, and useConsole.ReadLine
to read the user's command from the console - Repeat the command back to the user. For example, if they entered "1" as the command, the program should print "You entered: 1"
- Add an empty
Console.WriteLine
to make some space - Run the program, enter a command, and verify that it is properly remembered and repeated
string command = Console.ReadLine();
Console.WriteLine("You entered: " + command);
Console.WriteLine();
Executing the User Command
Now that the command is known, it is time to execute it. Currently, it is only possible to execute one command: the "1. Draw Bunny" command. To start, print out the word "bunny" if the user entered "1" for the command.
- After the command is read from the user, add an
if
statement - Within the conditional for the
if
statement, check if thecommand
variable is equal to"1"
- In the body of the
if
statement, useConsole.WriteLine
to write "bunny" to the console - Run the program, and verify that "bunny" is printed if the command entered is "1"
Code
if (command == "1")
{
Console.WriteLine("bunny");
}
Updating the Bunny
Instead of simply printing the word "bunny" to the console, the program should draw an ASCII art bunny. Use three Console.WriteLine
statements to draw a bunny. Run the program to see the new bunny!
Code
Console.WriteLine("() ()");
Console.WriteLine("(^ ^)");
Console.WriteLine("(___)");
Adding Another Command
So far, the program is not all that interesting. It can only execute one command! Add some code so that the program can execute another command: "2. Draw Fish"
- In the menu section, add another
Console.WriteLine
to print out "Draw Fish" option:Console.WriteLine("2. Draw Fish");
- Under the existing
if
statement, create a newif
statement - In this
if
statement, check if thecommand
variable is equal to"2"
- In the body of this
if
statement, write code to print out a small ASCII fish - Run the program, and verify that the fish is printed if the command entered is "2"
Code
if (command == "2")
{
Console.WriteLine(" _ ");
Console.WriteLine("><_>");
}
Looping the Process Infinitely
Currently, the program will run once, and then exit. However, to be a true menu-based application, the program should load the menu repetitively. Use a while
loop to accomplish this.
- Find the first empty
Console.WriteLine
statement, above the one that says "What would you like to do?" - Right above that statement, create a while loop
- Set the condition of the
while
loop to betrue
- This means the
while
loop will repeat infinitely!
- This means the
- Add the opening curly bracket immediately after the closing parenthesis for the
while
loop - Find the end of the second
if
statement, and add the closing curly bracket for thewhile
loop immediately following it - Indent the code within the
while
loop so it is easier to read - Run the program, and verify that the menu continuously re-appears!
Code
while (true)
{
Console.WriteLine();
Console.WriteLine("What would you like to do?");
Console.WriteLine();
Console.WriteLine("1. Draw Bunny");
Console.WriteLine("2. Draw Fish");
Console.WriteLine();
string command = Console.ReadLine();
Console.WriteLine();
if (command == "1")
{
Console.WriteLine("() ()");
Console.WriteLine("(^ ^)");
Console.WriteLine("(___)");
}
if (command == "2")
{
Console.WriteLine(" _ ");
Console.WriteLine("><_>");
}
}
Adding an Exit Command
To ensure that the program does not actually run forever, add an additional "3. Exit" command.
- In the menu section, add another
Console.WriteLine
to print out "Exit" option:Console.WriteLine("3. Exit");
- Under the existing
if
statement, create a newif
statement - In this
if
statement, check if thecommand
variable is equal to"3"
- In the body of this
if
statement, add abreak
command that will break out of thewhile
loop - Run the program, and verify that the program exits if the command entered is "2"
Code
if (command == "3")
{
break;
}