如何找到一种输出测验每个问题的尝试次数的方法?

How to find a way of output the number of attempts for each question of the quiz?

我正在控制台版本的 C# 上制作测验应用程序。
我几乎完成了所有事情,但我不知道如何在测验后显示每个问题的尝试次数完成了。如果你知道什么,请告诉我。
我无法添加更多代码行,因为网站不允许这样做

                    if (keys[index] == answer) // Answer is correct
                    {
                        Console.WriteLine();
                        Console.WriteLine("Congratulations. That's correct!");
                        Console.WriteLine();

                        totalScore += markPerQuestion;
                        index++;

                        Console.WriteLine("The total score is: {0}", totalScore);
                        Console.WriteLine("Used attempt(s): {0}", attempt);

                        attempt = 1;
                        count = attempt;
                        markPerQuestion = 20;

                       
                    }
                    else // Answer is incorrect
                    {
                        attempt++;
                        count++;

                        if (attempt <= 3)
                        {
                            markPerQuestion /= 2; 

                        }

                        else if (attempt > 3 && attempt < 5) // The fourth attempt gives zero points
                        {
                            markPerQuestion = 0;
                            totalScore += markPerQuestion;
                        }

                        else if(attempt >= 5)  // Move to the next question
                        {
                            Console.WriteLine("Sorry, you used all attempts for that question. Moving to the next question");
                            index++;
                            markPerQuestion = 20;
                            attempt = 1;
                            count = attempt;
                            continue;

                        }

                        Console.WriteLine("Oops, try again");
                    }
                    if ((index > keys.Length - 1 && index > questions.Length - 1)) // Questions and answer keys are finished
                    {
                            for (int i = 0; i < questions.Length; i++)
                            {
                                Console.WriteLine("Question {0} was answered after {1} attempt(s)", (i + 1), count);
                            }
                        
                        break;
                    }

考虑这个解决方案: 创建一个 public class 来存储结果。

public class QuizMark
{
    public int Attempts;
    public int Mark;
}

为控制台应用程序创建一个方法来控制测验。从 Main 方法调用方法 Quiz()

private const int MAX_ATTEMPTS = 5;
private static void Quiz()
{
    var quizResults = new List<QuizMark>();
    var questionAnswers = new List<int>() { 1, 3, 5, 2, 3, 6 };
    foreach(var a in questionAnswers)
    {
        var v = QuizQuestion(a);
        quizResults.Add(v);
    }

    int i = 0;
    quizResults.ForEach(e => Console.WriteLine($"Question: {++i}  Attempts: {e.Attempts}  Mark: {e.Mark}"));

    var total = quizResults.Sum(s => s.Mark);
    Console.WriteLine($"Total Points: {total}");
}

请注意存储 class QuizMark 对象的 List 集合。这是存储每个问题的结果的地方:尝试和分数。 List questionAnswers 仅包含每个问题的预期答案。

现在创建方法来控制如何处理测验中的每个问题:

    private static QuizMark QuizQuestion(int answer)
    {
        var quizMark = new QuizMark();

        int guess = 0; //Store ReadLine in this variable
        int mark = 20;

        for (int attempt = 1; attempt < MAX_ATTEMPTS + 1; attempt++)
        {
            guess++; //remove when adding Console.ReadLine
            if (guess.Equals(answer))
            {
                quizMark.Attempts = attempt;
                quizMark.Mark = mark;
                break;
            }
            else
            {
                mark = attempt <= 3 ? mark/2 : 0;
                quizMark.Attempts = attempt;
                quizMark.Mark = mark;
            }
        }

        return quizMark;
    }

您需要将增量器 guess++ 替换为用户的实际猜测。此代码旨在作为演示自动通过。

重要提示:
每当您允许用户输入数据时,您都需要进行一些错误处理。他们可能会输入非整数值。可能使用围绕 Console.ReadLine 的循环,您可以在其中使用 Int32.TryParse().

检查输入值