Solutions for those exercises.
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”.
Questions
-
What is string interpolation?
-
What is the difference, if any, between the
WriteLine
andWrite
methods? -
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}");
-
Is the name myVariable the same as myvariable? If not, why?
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 . . .
- 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.
- Declare a