About Me Code-Along: Hard-Coded Values
Create a program that stores some information about a user, and then prints it out in a sentence format. Start by creating a new C# Repl, and then follow the steps below.
Name Variable
First, declare and set a new variable. This variable will hold a name; feel free to use your own name, or any other name you'd like.
- Remove any code from the
Main
code block - Create a new
string
variable nameduserName
withstring userName
- Set the value of the variable to a new string with
= ""
- Fill out the name between the opening and closing quotation marks
- Add a semi-colon at the end
string userName = "Jordan Conrad";
Other Variables
After the first variable is declared and set, it's time to add some additional variables. Remember to end each statement with a semi-colon!
- Make a new line under the first variable
- Declare a new
int
variable nameduserAge
, and set it equal to any age (e.g.27
) - Make another new line
- Declare another
int
variable namednumberOfPets
, and set it equal to any number (e.g.2
) - Make another new line
- Declare a
string
variable namedschool
, and set it equal to a value between quotes (""
) - Fill out a value for the
school
variable (e.g."Medina High School"
)
Run the program, and make sure there are no errors.
Note: There may be warnings saying that these variables are never used - that is to be expected at this point
int userAge = 27;
int numberOfPets = 2;
string school = "Medina High School";
Displaying the Name Information
Now that the information is properly stored in variables, try displaying it in a sentence format. Start with the userName
value.
- Make a new line under the variables
- Create a
Console.WriteLine();
statement to display a message - Within the parentheses for the
Console.WriteLine();
, add a set of double quotes (""
) - Within the double quotes, type out "Hello! My name is "
- Make sure to include the space
- After the ending quote, add a
+
and theuserName
variable
Run the program, and verify that the sentence appears in the console!
Console.WriteLine("Hello! My name is " + userName);
Displaying the Other Information
Next, display the rest of the information.
- Under the previous statement, add another
Console.WriteLine();
- Within the parentheses, add a string that says "I am "
- After the quote, use
+
to add theuserAge
variable - After the variable, use
+
again to complete the message with " years old"
Console.WriteLine("I am " + userAge + " years old");
- Under that, add another
Console.WriteLine();
- Within the parentheses, add a string that says "I have "
- After the quote, use
+
to add thenumberOfPets
variable - After the variable, use
+
again to complete the message with " pets
Console.WriteLine("I have " + numberOfPets + " pets");
- Under that, add another
Console.WriteLine();
- Within the parentheses, add a string that says "I went to school at "
- After the quote, use
+
to add theschool
variable
Console.WriteLine("I went to school at " + school);
Run the code, and make sure all the information is displayed properly!
Final Code
string userName = "Jordan Conrad";
int userAge = 27;
int numberOfPets = 2;
string school = "Medina High School";
Console.WriteLine("Hello! My name is " + userName);
Console.WriteLine("I am " + userAge + " years old");
Console.WriteLine("I have " + numberOfPets + " pets");
Console.WriteLine("I went to school at " + school);