Warm-up Exercises
-
Assuming that
myLastName
andmyFirstName
are two string variables that have been initialized, write a statement that concatenates them, with a space and a comma in-between, and assign the resulting string to a variable namedfullName
. For instance, if the value ofmyLastName
is “Holbertonand”, and the value ofmyFirstName
is “Betty”, then the value offullName
after your operation should be “Holbertonand, Betty”.Solution
string fullName = $"{myLastName}, {myFirstName}";
Questions
-
What is string interpolation?
Solution
String interpolation is the use of specialized syntax (
$
and curly braces) to include variables within a string. -
What is the difference, if any, between the
WriteLine
andWrite
methods?Solution
The
WriteLine
method appends a newline character to the end of the argument passed into it whileWrite
does not. -
Assume we have a variable whose name is
myVariable
, whose type isstring
, and whose value is “My message”. What would be displayed on the screen by the following statement?Console.WriteLine($"Here is my variable: {myVariable}");
Solution
“Here is my variable: My message”
-
Is the name myVariable the same as myvariable? If not, why?
Solution
The variable names myVariable and myvariable are different because variable names are case sensitive.
Problems
-
Write down, on a piece of paper, a program that:
- Declares a
string
variable nameduserName
. - Displays on the screen:
Please, enter your name, followed by the enter key:
. - Reads a
string
value from the keyboard and assigns the value to theuserName
variable. - Declares an
int
variable namednumber
. - Displays on the screen:
Please, enter your area code, followed by the enter key:
. - Reads an
int
value from the keyboard and assigns the value to thenumber
variable. - Declares a
string
variable namedid
and initializes it with the string referenced by theuserName
variable, followed by the number entered by the user. (Note: you can cocatenate astring
and anint
using the+
sign.) - Displays on the screen:
Your id is
, followed by the content of theid
variable.
Here is an example of execution, where the user input is underlined, and hitting “enter” is represented by ↵:
Please, enter your name, followed by enter: C͟l͟é͟m͟e͟n͟t͟ ↵ Please, enter your area code, followed by enter: 8͟2͟8͟ ↵ Your ID is Clément828 Press any key to continue . . .
Solution
string userName; Console.WriteLine("Please enter your name, followed by enter."); userName = Console.ReadLine(); int number; Console.WriteLine("Please enter your area code, followed by enter."); number = int.Parse(Console.ReadLine()); string id = userName + number; Console.WriteLine("Your ID is " + id);
- Declares a
-
Write a series of statements that:
- Declare a
string
variable namedfavoriteColor
- Display on the screen a message asking the user his or her favorite color
- Read the value entered by the user and store it in the
favoriteColor
variable.
You can combine some of the statements if you want, but do not display on the screen any information that was not explicitly asked.
Solution
string favoriteColor; Console.Write("Please enter your favorite color: "); favoriteColor = Console.ReadLine();
- Declare a