About You Code-Along: Asking the User for Values
Create a program that asks the user for information, and then prints it out in a sentence format. Start by creating a new C# Repl, and then follow the steps below.
Asking for a Name
First, ask the user for their name.
- Remove any existing code within the
Main
block - Add a
Console.WriteLine
statement to print a message: "What is your name?" - Under the
Console.WriteLine
, add aConsole.ReadLine()
statement - Store the answer in a new
string
variable nameduserName
Run the program and make sure there are no errors.
Console.WriteLine("What is your name?");
string userName = Console.ReadLine();
Asking for an Age
Next, ask the user how old they are. This will be used to calculate their upcoming birthday age.
- Make a new line under the previous statement
- Add a
Console.WriteLine
statement to print a message: "How old are you?" - Under the
Console.WriteLine
, add aConsole.ReadLine()
statement - Wrap the
Console.ReadLine()
in aConvert.ToInt32()
so the result becomes a number - Store the answer in a new
int
variable nameduserAge
- Under that, set another variable named
userNextAge
to the user's current age plus one
Run the program and make sure there are no errors.
Console.WriteLine("How old are you?");
int userAge = Convert.ToInt32(Console.ReadLine());
int userNextAge = userAge + 1;
Asking for Pets
Next, ask the user how many pets they have.
- Make a new line under the previous statement
- Add a
Console.WriteLine
statement to print a message: "How many pets do you have?" - Under the
Console.WriteLine
, add aConsole.ReadLine()
statement - Wrap the
Console.ReadLine()
in aConvert.ToInt32()
so the result becomes a number - Store the answer in a new
int
variable namednumberOfPets
Run the program and make sure there are no errors.
Console.WriteLine("How many pets do you have?");
int numberOfPets = Convert.ToInt32(Console.ReadLine());
Asking for School
Next, ask the user which school they attend.
- Make a new line under the previous statement
- Add a
Console.WriteLine
statement to print a message: "How many pets do you have?" - Under the
Console.WriteLine
, add aConsole.ReadLine()
statement - Store the answer in a new
string
variable namedschool
Run the program and make sure there are no errors.
Console.WriteLine("What school do you go to?");
string school = Console.ReadLine();
Printing the Results
Finally, it's time to print out all of the information!
- Make a new line under the previous statement
- Create a
Console.WriteLine
statement - Set the message to say "Hello! My name is "
- Add the
userName
variable to the end of the message string using+
- Under that statement, create another
Console.WriteLine
- Set the message to say "I am "
- Add the
userAge
variable to the end of the message string using+
- Add " years old" to the end of the message string using
+
- Under that statement, create another
Console.WriteLine
- Set the message to say "Next year I will be "
- Add the
userNextAge
variable to the end of the message string using+
- Under that statement, create another
Console.WriteLine
- Set the message to say "I have "
- Add the
numberOfPets
variable to the end of the message string using+
- Add " pets" to the end of the message string using
+
- Under that statement, create another
Console.WriteLine
- Set the message to say "I attend "
- Add the
school
variable to the end of the message string using+
Run the program and make sure that all the proper information is displayed!
Console.WriteLine("Hello! My name is " + userName);
Console.WriteLine("I am " + userAge + " years old");
Console.WriteLine("Next year I will be " + userNextAge);
Console.WriteLine("I have " + numberOfPets + " pets");
Console.WriteLine("I attend " + school);
Final Code
Console.WriteLine("What is your name?");
string userName = Console.ReadLine();
Console.WriteLine("How old are you?");
int userAge = Convert.ToInt32(Console.ReadLine());
int userNextAge = userAge + 1;
Console.WriteLine("How many pets do you have?");
int numberOfPets = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What school do you go to?");
string school = Console.ReadLine();
Console.WriteLine("Hello! My name is " + userName);
Console.WriteLine("I am " + userAge + " years old");
Console.WriteLine("Next year I will be " + userNextAge);
Console.WriteLine("I have " + numberOfPets + " pets");
Console.WriteLine("I attend " + school);