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.

  1. Remove any existing code within the Main block
  2. Add a Console.WriteLine statement to print a message: "What is your name?"
  3. Under the Console.WriteLine, add a Console.ReadLine() statement
  4. Store the answer in a new string variable named userName

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.

  1. Make a new line under the previous statement
  2. Add a Console.WriteLine statement to print a message: "How old are you?"
  3. Under the Console.WriteLine, add a Console.ReadLine() statement
  4. Wrap the Console.ReadLine() in a Convert.ToInt32() so the result becomes a number
  5. Store the answer in a new int variable named userAge
  6. 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.

  1. Make a new line under the previous statement
  2. Add a Console.WriteLine statement to print a message: "How many pets do you have?"
  3. Under the Console.WriteLine, add a Console.ReadLine() statement
  4. Wrap the Console.ReadLine() in a Convert.ToInt32() so the result becomes a number
  5. Store the answer in a new int variable named numberOfPets

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.

  1. Make a new line under the previous statement
  2. Add a Console.WriteLine statement to print a message: "How many pets do you have?"
  3. Under the Console.WriteLine, add a Console.ReadLine() statement
  4. Store the answer in a new string variable named school

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!

  1. Make a new line under the previous statement
  2. Create a Console.WriteLine statement
  3. Set the message to say "Hello! My name is "
  4. Add the userName variable to the end of the message string using +
  5. Under that statement, create another Console.WriteLine
  6. Set the message to say "I am "
  7. Add the userAge variable to the end of the message string using +
  8. Add " years old" to the end of the message string using +
  9. Under that statement, create another Console.WriteLine
  10. Set the message to say "Next year I will be "
  11. Add the userNextAge variable to the end of the message string using +
  12. Under that statement, create another Console.WriteLine
  13. Set the message to say "I have "
  14. Add the numberOfPets variable to the end of the message string using +
  15. Add " pets" to the end of the message string using +
  16. Under that statement, create another Console.WriteLine
  17. Set the message to say "I attend "
  18. 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);

results matching ""

    No results matching ""