๐ Instructions#
Now that the player’s choice is saved as a number, we can use it to display a message confirming what they picked.
To do that, the program needs to make a decision based on the value of playerChoice. This is done with an if/else statement.
int score = 10;
if (score == 10)
{
Console.WriteLine("You got a perfect score!");
}
else
{
Console.WriteLine("You didn't get a perfect score.");
}score = 10
if score == 10:
print("You got a perfect score!")
else:
print("You didn't get a perfect score.")let score = 10;
if (score === 10) {
console.log("You got a perfect score!");
} else {
console.log("You didn't get a perfect score.");
}Here’s how it works:
ifchecks whether the condition inside the parentheses is true- If it is, the first block runs
- If it isn’t, the
elseblock runs instead - Only one of the two blocks ever runs โ never both
The condition uses == to check if two values are equal. This is different from =, which assigns a value to a variable. Using one when you mean the other is a common mistake.
๐ Learn More
โ What to Do#
- After parsing the player’s input, write an
if/elsestatement that printsYou chose Odds.ifplayerChoiceequals1, orYou chose Evens.if it doesn’t. - Run the program and test both inputs.
๐ฏ Expected Outcome#
Welcome to Odds and Evens!
Choose 1 for Odds or 2 for Evens:
1
You chose Odds.Welcome to Odds and Evens!
Choose 1 for Odds or 2 for Evens:
2
You chose Evens.๐ก Hints#
Hint 1
The if/else block goes right after the two lines that read and parse the player’s input.
Hint 2
The condition should check whether playerChoice equals 1. Remember: use == to compare, not =.
Hint 3
Each block needs its own Console.WriteLine with the correct message inside.
๐ 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:");
string input = Console.ReadLine();
int playerChoice = int.Parse(input);
++
++ if (playerChoice == 1)
++ {
++ Console.WriteLine("You chose Odds.");
++ }
++ else
++ {
++ Console.WriteLine("You chose Evens.");
++ }
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 input = Console.ReadLine();
int playerChoice = int.Parse(input);
if (playerChoice == 1)
{
Console.WriteLine("You chose Odds.");
}
else
{
Console.WriteLine("You chose Evens.");
}
(...)