Solutions for those exercises.
Warm-up Exercises
- Assuming that
myLastNameandmyFirstNameare 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 ofmyLastNameis “Holbertonand”, and the value ofmyFirstNameis “Betty”, then the value offullNameafter your operation should be “Holbertonand, Betty”.
Questions
-
What is string interpolation?
-
What is the difference, if any, between the
WriteLineandWritemethods? -
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
stringvariable nameduserName. - Displays on the screen:
Please, enter your name, followed by the enter key:. - Reads a
stringvalue from the keyboard and assigns the value to theuserNamevariable. - Declares an
intvariable namednumber. - Displays on the screen:
Please, enter your area code, followed by the enter key:. - Reads an
intvalue from the keyboard and assigns the value to thenumbervariable. - Declares a
stringvariable namedidand initializes it with the string referenced by theuserNamevariable, followed by the number entered by the user. (Note: you can cocatenate astringand anintusing the+sign.) - Displays on the screen:
Your id is, followed by the content of theidvariable.
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
stringvariable 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
favoriteColorvariable.
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