π Instructions#
Right now the program reads what the player types but immediately forgets it. We need to hold on to that value so the game can use it later.
That’s what a variable is for. A variable gives a name to a stored value. You can read it, change it, or pass it along β as long as the program is running, it remembers.
Some functions don’t just do something β they also hand a value back when they finish. To keep that value, assign it to a variable:
string input = Console.ReadLine();The data type before the variable name tells the program what kind of value to expect. string means text.
π§ Recall
π Learn More
β What to Do#
Update the line that calls Console.ReadLine() so its result is stored in a variable named playerChoice.
π― Expected Outcome#
The output looks exactly the same as before. Storing a value in a variable produces no visible output β but the value is now saved and ready for the next step.
Welcome to Odds and Evens!
Choose 1 for Odds or 2 for Evens:
1
π‘ Hints#
Hint 1
Find the line with the input-reading function call. That’s the only line that needs to change.
Hint 2
You can capture what a function returns by writing variableType variableName = before the function call.
Hint 3
The player is typing text, so the variable type is string. The variable name should be playerChoice.
β οΈ Common Mistakes#
Forgetting the type declaration
Writing playerChoice = Console.ReadLine(); without the string type causes a compile error. In C#, you must declare the type when creating a new variable.
Using the wrong type
Writing int playerChoice = Console.ReadLine(); causes a compile error. Console.ReadLine() always returns text β even if the player types 1, it comes back as the string "1", not the number 1. Use string here.
Leaving the old line unchanged
If you add a new line instead of replacing the existing Console.ReadLine();, you’ll read input twice β the program will pause and wait for the player to type two things. Replace the old line, don’t duplicate it.
π Solution#
Tried, you must, before reveal the solution you may.
Program.csChanges
// Player chooses to play with odds or evens.
Console.WriteLine("Choose 1 for Odds or 2 for Evens:");
- Console.ReadLine();
+ string playerChoice = Console.ReadLine();
Program.csFinal
Console.WriteLine("Welcome to Odds and Evens!");
// Player chooses to play with odds or evens.
Console.WriteLine("Choose 1 for Odds or 2 for Evens:");
string playerChoice = Console.ReadLine();
(...)