如何显示已回答了多少问题? C#控制台应用程序

时间:2018-01-29 01:35:44

标签: c#

有点问题......我想知道在哪个方面可以显示已回答的问题数量?如果向下滚动,您将看到目前为止存在的问题。在此之下,您将看到我做了什么。如果你能帮助我,我想要它。我绝对不是编程方面的专家,我知道这些功能是如何工作的,但是不太好......

                var questions = new List<Question>()

    {
        new Question { Text = "Q. [Attack On Titan] Which character is the 'Rogue' Titan?", Answer = "Eren"},
        new Question { Text = "Q. [Pokemon] Which Pokemon does Ash use mostly?", Answer = "Pikachu" },
        new Question { Text = "Q. [Fairy Tail] Who raised Natsu Dragneel when he was a child?", Answer = "Igneel" },
        new Question { Text = "Q. [Death Note] What was Light's surname?", Answer = "Yagami" },
        new Question { Text = "Q. [Attack On Titan] Who was Eren's best friend?", Answer = "Armin" },
        new Question { Text = "Q. [Attack On Titan] Which character is the'Armored' Titan?", Answer = "Reiner" },
        new Question { Text = "Q. [Attack On Titan] Which character is the 'Colossal' Titan?", Answer = "Bertholt" },
        new Question { Text = "Q. [Death Note] In the series, there was always a shinigami with Light Yagami, what was their name?", Answer = "Ryuk" },
        new Question { Text = "Q. [Attack On Titan] Who gave Mikasa their red scarf?", Answer = "Eren" },
    };

                Random random = new Random();

                foreach (var question in questions.OrderBy(q => random.Next()))
                {
                    Console.WriteLine(question.Text);

                    do
                    {
                        var answer = Console.ReadLine();
                        if (question.IsCorrect(answer))
                        {
                            Console.WriteLine("That is correct!" + 1 / 100); // My attempt :[
                            Thread.Sleep(800);
                            Console.Clear();

                        Console.WriteLine("You are incorrect.");
                    }
                    while (true);
                }

1 个答案:

答案 0 :(得分:0)

foreach循环之外创建一个整数变量,以跟踪正确回答的问题数量,如果用户得到问题,foreach循环增加该变量,那么你可以将变量打印到控制台。

一个例子:

int correctAnswers = 0;

foreach(var question in questions.OrderBy(q => random.Next()))
{
    Console.WriteLine(question.Text);
    string answer = "";

    do
    {
        answer = Console.ReadLine();

        if (question.IsCorrect(answer))
        {
            Console.WriteLine($"That is correct! {++correctAnswers}/100");

            Thread.Sleep(800);
            Console.Clear();

            Console.WriteLine("You are incorrect.");
         }
    }
    while (string.IsNullOrWhiteSpace(answer));
}