为什么String.Equals不起作用?

时间:2018-05-15 23:41:42

标签: c# string comparison

我的朋友和我正在研究一个简单的琐事控制台应用程序,试图学习编码C#。我们有随机问题生成器工作,我们可以接受输入。我们遇到了一个问题,无论我们输入什么代码总是说这是错误的答案。我们希望它说正确的答案或错误的答案就像普通的琐事那样。要检查答案是否正确或不正确,我们使用String.Equals来检查答案是否与存储在数组中的答案相匹配。我们很好奇我们在这里做错了什么,以及如何使if语句正常工作。

static void Main(string[] args)
    {
        //Random number generator
        Random rnd = new Random();
        int randInt = rnd.Next(0, 9);

        //Questions array Question/Answer
        string[,] array2D = new string[10, 2] {
            { "Mario Kart is a video game series publish by which company?", "Nintendo" },
            { "What was the first console video game that allowed the game to be saved?", "The Legend of Zelda" },
            { "The Connecticut Leather Company later became what toy company that was popular in the 1980s for its Cabbage Patch Kids and video game consoles?", "Coleco" },
            { "Nintendo is a consumer electronics and video game company founded in what country?", "Japan" },
            { "The first person shooter video game Doom was first released in what year?", "1993" },
            { "In what year did Nintendo release its first game console in North America?", "1985" },
            { "In the world of video games, what does NES stand for?", "Nintendo Entertainment System" },
            { "In what year was the Nintendo 64 officially released?", "1996" },
            { "What was the most popular video game in the year 1999?", "Doom" },
            { "If you have any kind of console known to mankind, what will your mother call it no matter what?", "A Nintendo" } };

        //printing the question
        System.Console.WriteLine(array2D[randInt, 0]);
        System.Console.ReadKey();

        //taking user input
        string Answer = Console.ReadLine();

        //checking to see if the answer is correct
        if (Answer.Equals(array2D[randInt, 1]))
        {
            //the console exclamation for the right answer given in any scenario.
            Console.WriteLine("Correct Answer! Good job!");
            Console.ReadKey();
        }
        else
        {
            //the console exlamation for the incorrect answer in any given scnario
            Console.WriteLine("Aw man! You got it wrong! Better luck next time :(");
            Console.WriteLine("The correct answer was {0}", array2D[randInt, 1]);
            Console.ReadKey();
        }

    }

1 个答案:

答案 0 :(得分:2)

只需从上方移除System.Console.ReadKey();

//Random number generator
        Random rnd = new Random();
        int randInt = rnd.Next(0, 9);

        //Questions array Question/Answer
        string[,] array2D = new string[10, 2] {
        { "Mario Kart is a video game series publish by which company?", "Nintendo" },
        { "What was the first console video game that allowed the game to be saved?", "The Legend of Zelda" },
        { "The Connecticut Leather Company later became what toy company that was popular in the 1980s for its Cabbage Patch Kids and video game consoles?", "Coleco" },
        { "Nintendo is a consumer electronics and video game company founded in what country?", "Japan" },
        { "The first person shooter video game Doom was first released in what year?", "1993" },
        { "In what year did Nintendo release its first game console in North America?", "1985" },
        { "In the world of video games, what does NES stand for?", "Nintendo Entertainment System" },
        { "In what year was the Nintendo 64 officially released?", "1996" },
        { "What was the most popular video game in the year 1999?", "Doom" },
        { "If you have any kind of console known to mankind, what will your mother call it no matter what?", "A Nintendo" } };

        //printing the question
        System.Console.WriteLine(array2D[randInt, 0]);
        //System.Console.ReadKey(); Removed

        //taking user input
        string Answer = Console.ReadLine();

        //checking to see if the answer is correct
        if (Answer.Equals(array2D[randInt, 1]))
        {
            //the console exclamation for the right answer given in any scenario.
            Console.WriteLine("Correct Answer! Good job!");
            Console.ReadKey();
        }
        else
        {
            //the console exlamation for the incorrect answer in any given scnario
            Console.WriteLine("Aw man! You got it wrong! Better luck next time :(");
            Console.WriteLine("The correct answer was {0}", array2D[randInt, 1]);
            Console.ReadKey();
        }
相关问题