Show Player Choice

πŸ“– 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 that value.

That’s what an if/else statement does β€” it checks a condition and runs different code depending on whether the condition is true or false. Conditions are built using comparison operators, which compare two values and produce a true or false result.

🧠 Recall

int score = 10;

if (score == 10)
{
    Console.WriteLine("You got a perfect score!");
}
else
{
    Console.WriteLine("You didn't get a perfect score.");
}

πŸŽ“ Learn More

βœ… What to Do#

After parsing the player’s input, write an if/else statement that prints You chose Odds. if the player chose 1, or You chose Evens. otherwise. 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 lines that read and parse the player’s input.

Hint 2

The condition should check whether the player’s choice equals 1. Use the equality operator, not the assignment operator.

Hint 3

Each branch needs its own print statement with the correct message inside.

⚠️ Common Mistakes#

Using = instead of == in the condition

= assigns a value to a variable. == checks if two values are equal. Using = inside an if condition is a compile error in C#.

Putting the same message in both branches

Copy-pasting the print statement into both branches and forgetting to change one of the messages is easy to miss. Run the program with both inputs to verify each branch prints the correct text.

Only handling the if branch

Writing only the if block without an else means the program prints nothing when the player chooses 2. Always consider what should happen in the other case.

Checking the wrong variable

Make sure the condition checks playerChoice β€” the parsed integer β€” not input, which is still a string. Comparing a string to a number will either fail or produce unexpected results.

πŸ™ˆ 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.");
}

(...)