samedi 4 mai 2019

C# seeing if user entered a question before doing action

I want the user to enter a question. They can type "A" and ask a question. If they type "S" then it will shake the list of answers. If they type "S" first before they enter a question then they will get an error message.

I'm having trouble with my if else statements. I can't seem to figure out if they enter a question before they shake.

Program.cs:

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();

     public string userAnswer

     do
     {
        if (input == "S")
        {
            if (userAnswer != null)
            {
                Console.WriteLine("Searching the Mystic Realms(RAM) for the answer");
            }
            else
            {
                //Call Method Shake()
            }
        }
        else if (input == "A") {
                userAnswer = Console.ReadLine();
        }
        else if (input == "G") {
               //Call Method GetAnswer()
        }
    } while (input != "E");
}

Magic8Ball.cs

public void Shake()
    {
        //picking the index of the answer to show the user
        Random r = new Random();
        int index = r.Next(_answers.Count);
        randomString = _answers[index];
    }

    public string GetAnswer()
    {
        //using the index picked by shake to return the answer
        //return "";
        return randomString;
    }
    }

Aucun commentaire:

Enregistrer un commentaire