dimanche 5 mai 2019

If Loop Not working Correctly Won't Pass If Statement

I'm making a magic 8 ball within the cmd. I want to ask the user if they would like to do. I want the program to keep asking questions until the user selects the letter E. If they try to shake before they ask a question then they will get an error.

The issue that I'm having is that when you enter A, you will enter a question. Then when I enter S right after, I get the error message searching RAM and it doesn't call my shake method.

public static string userAnswer = "";

    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")
            {
                if (userAnswer != null)
                {
                    Console.WriteLine("Searching the Mystic Realms(RAM) for the answer");
                    Console.WriteLine("(S)hake the Ball");
                    Console.WriteLine("(A)sk a Question");
                    Console.WriteLine("(G)et the Answer");
                    Console.WriteLine("(E)xit the Game");
                    input = Console.ReadLine();
                }
                else
                {
                    //Call Method Shake()
                    ball.Shake();
                    Console.WriteLine("(S)hake the Ball");
                    Console.WriteLine("(A)sk a Question");
                    Console.WriteLine("(G)et the Answer");
                    Console.WriteLine("(E)xit the Game");
                    input = Console.ReadLine();
                }
            }
            else if (input == "A")
            {
                userAnswer = Console.ReadLine();
                Console.WriteLine("(S)hake the Ball");
                Console.WriteLine("(A)sk a Question");
                Console.WriteLine("(G)et the Answer");
                Console.WriteLine("(E)xit the Game");
                input = Console.ReadLine();
            }
            else if (input == "G")
            {
                if (userAnswer != null)
                {
                    Console.WriteLine("Please Enter A Question Before Asking For An Answer.");
                    Console.WriteLine("(S)hake the Ball");
                    Console.WriteLine("(A)sk a Question");
                    Console.WriteLine("(G)et the Answer");
                    Console.WriteLine("(E)xit the Game");
                    input = Console.ReadLine();
                }
                else
                {
                    //Call Method GetAnswer()
                    ball.GetAnswer();
                    Console.WriteLine("(S)hake the Ball");
                    Console.WriteLine("(A)sk a Question");
                    Console.WriteLine("(G)et the Answer");
                    Console.WriteLine("(E)xit the Game");
                    input = Console.ReadLine();
                }
            }
     }while (input != "E");
    }
}

}

Aucun commentaire:

Enregistrer un commentaire