What I'm trying to create is a Magic 8 Ball. If the user asks to shake the ball before they ask a question then they get an error. If they ask the question (By clicking A) and then ask to shake it (By clicking S) they will invoke my method that will shake a list of answers. I'm not trying to print the answer in this part.
The issue that I'm having is I'm not too sure how to see if the user entered a certain key.
namespace Magic8Ball_Console
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main program!");
Console.WriteLine("Welcome to the Magic 8 Ball");
Console.WriteLine("What would you like to do?");
Console.WriteLine("(S)hake the Ball");
Console.WriteLine("(A)sk a Question");
Console.WriteLine("(G)et the Answer");
Console.WriteLine("(E)xit the Game");
Magic8Ball_Logic.Magic8Ball ball = new Magic8Ball_Logic.Magic8Ball();
string input = Console.ReadLine().ToUpper();
do
{
if (input == "S")
{
Console.WriteLine("Searching the Mystic Realms(RAM) for the answer");
Console.ReadLine();
}
else if (input == "A") {
//Call Method Shake()
Console.ReadLine();
}
} while (input != "E");
}
}
}
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)