如果语句不能正常工作,则无法通过如果语句C#

时间:2019-05-05 14:39:44

标签: c# if-statement

我正在cmd内制作一个魔术8球。我想问用户是否愿意。我希望程序在用户选择字母E之前一直询问问题。如果在提出问题之前尝试摇动,则会出现错误。

我遇到的问题是,当您输入A时,您将输入一个问题。然后,当我紧接着输入S时,我得到了错误消息,搜索RAM,它没有调用我的摇晃方法。

所以现在发生的是我写了“ A”。然后输入我的问题,选项再次出现。一旦选项再次显示,我选择“ S”,然后出现ram语句,而不是转到else语句。我需要它进入摇动方法,这样我才能键入“ G”以得到答案。

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");
}

1 个答案:

答案 0 :(得分:2)

您正在声明中设置{$gt: {timestamp: $PREVIOUS_TIMESTAMP}}。但是..如果您正在string userAnswer = "";上进行检查,但是...它永远不会为空

您应该检查if (userAnswer != null),因为如果字符串的值if (userAnswer != "")与空字符串null

不同