C
C#8mo ago
Mekasu0124

✅ Switch Case Not Executing Properly

public static void ShowMenu(string username, string date)
{
string choice = GetMenuChoice();
string difficulty = GetGameDifficulty();
int maxQuestions = GetNumberOfQuestions();

switch (choice)
{
case "Addition":
case "Subtraction":
case "Multiplication":
case "Division":
case "Random":
Game.StartGame(username, date, choice, difficulty, maxQuestions);
break;

case "Previous Games":
Game.ShowPreviousGames();
break;

case "Quit Game":
ColorCon.WriteLine("Exiting Application", ConsoleColor.Red);
Environment.Exit(0);
break;
}
}

public static string GetMenuChoice()
{
Console.Clear();
string menuOptions = """
What Would You Like To Do?

A - Addition
S - Subtraction
M - Multiplication
D - Division
R - Random
P - Previous Games
Q - Quit Game


Your Selection:
""";
List<string> choices = new() { "a", "s", "m", "d", "r", "p", "q" };
string errorText = "Invalid Input. Choice Must Be: \"A\", \"S\", \"M\", \"D\", \"R\", \"P\", or \"Q\"";

string choice = ColorCon.GetStringFromConsole(
prompt: menuOptions,
color: ConsoleColor.Cyan,
validator: x => choices.Contains(x.ToLower()),
errorMessage: errorText);

return choice switch
{
"a" or "A" => "Addition",
"s" or "S" => "Subtraction",
"m" or "M" => "Multiplication",
"d" or "D" => "Division",
"r" or "R" => "Random",
"p" or "P" => "Previous Games",
"q" or "Q" => "Quit Game",
_ => throw new Exception("Sum Ting Wong")
};
}
public static void ShowMenu(string username, string date)
{
string choice = GetMenuChoice();
string difficulty = GetGameDifficulty();
int maxQuestions = GetNumberOfQuestions();

switch (choice)
{
case "Addition":
case "Subtraction":
case "Multiplication":
case "Division":
case "Random":
Game.StartGame(username, date, choice, difficulty, maxQuestions);
break;

case "Previous Games":
Game.ShowPreviousGames();
break;

case "Quit Game":
ColorCon.WriteLine("Exiting Application", ConsoleColor.Red);
Environment.Exit(0);
break;
}
}

public static string GetMenuChoice()
{
Console.Clear();
string menuOptions = """
What Would You Like To Do?

A - Addition
S - Subtraction
M - Multiplication
D - Division
R - Random
P - Previous Games
Q - Quit Game


Your Selection:
""";
List<string> choices = new() { "a", "s", "m", "d", "r", "p", "q" };
string errorText = "Invalid Input. Choice Must Be: \"A\", \"S\", \"M\", \"D\", \"R\", \"P\", or \"Q\"";

string choice = ColorCon.GetStringFromConsole(
prompt: menuOptions,
color: ConsoleColor.Cyan,
validator: x => choices.Contains(x.ToLower()),
errorMessage: errorText);

return choice switch
{
"a" or "A" => "Addition",
"s" or "S" => "Subtraction",
"m" or "M" => "Multiplication",
"d" or "D" => "Division",
"r" or "R" => "Random",
"p" or "P" => "Previous Games",
"q" or "Q" => "Quit Game",
_ => throw new Exception("Sum Ting Wong")
};
}
no matter what I enter in for my choice, the switch case in the top function always runs Game.StartGame(). What do I have wrong?
1 Reply
Mekasu0124
Mekasu01248mo ago
when I Console.WriteLine(choice); it's returning the correct option that I chose in the second function, but the switch statement is giving the issue and I don't know why I have tried breaking the switch case down into each case having it's own code block. I have trie doing case "Addition" or "Subtraction" or "Multiplication ... and that doesn't fix it either nvm I'm dumb