[Solved] C# See If A Certain Key is Entered


Since you have read the user input into the variable input, check its content

string input = Console.ReadLine().ToUpper();
switch (input) {
    case "S":
        //TODO: Shake the Ball
        break;
    case "A":
        //TODO: Ask a question
        break;
    case "G":
        //TODO: Get the answer
        break;
    case "E":
        //TODO: Exit the game
        break;
    default:
       // Unknown input
       break;
}

Note, if you have to differentiate between many cases, it’s usually easier to use switch than a lot of if-else statements.

I converted the input to upper case, so that the user can enter the command as lower or upper case.

You will have to use some loop, if you don’t want the game to exit after the first command has been processed. E.g.

do {
    // the code from above here
} while (input != "E");

See also: switch (C# reference)

solved C# See If A Certain Key is Entered