Search code examples
c#console

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


I am making the quiz application on C# in Console version.
I have almost done all things, but I don't know how to show the number of attempts for each question, after when the quiz is finished. If you know something, let me know.
I can not add more lines of the code, as the website doesn't allow to do it

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

Solution

  • Consider this solution: Create a public class that will allow you to store the results.

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

    For the Console app create a method to control the Quiz. Call the method Quiz() from the Main method.

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

    Notice the List collection that stores an object of the class QuizMark. This is where the results of each question are stored: attempts and points. The List questionAnswers simply contains the expected answer to each of the questions.

    Now create the method that is going to control how each question in the quiz will be handled:

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

    You will need to replace the incrementor guess++ with the actual guess the user makes. This code is designed to go though automatically just as a demonstration.

    IMPORTANT NOTE:
    You will want to do some error handling any time you allow users to enter data. They might enter non-integer values. Probably using a loop around a Console.ReadLine where you check the value of the input with a Int32.TryParse().