How to Code a Quiz App in C#

Find the source code for this project here

Step 1: Setting Up the Questions

We're going to create an array to hold our questions:

string[] questions =
{
    "What book holds the record for the fastest selling book in history?",
    "How old was Queen Elizabeth II when she was first crowned the Queen of England?",
    "Which blood type is a universal donor?"
};

Step 2: Adding Answers

Let's make things more exciting by adding answer options for each question:

string[] answers =
{
    "A. The Hunger Games \nB. Harry Potter and the Dealthy Hallows \nC. To Kill A Mockingbird",
    "A. 27 \nB. 24 \nC. 31",
    "A. O negative \nB. B Positive \nC. AB"
};

Step 3: Correct Answers Rule

We're keeping track of the correct answers with an array:

int[] correctAnswers = { 1, 1, 0 };

Step 4: Player Score Initiated

Let's set up a variable to track the player's score:

int playerScore = 0;

Step 5: Welcoming the Player

Welcome the players with a friendly message:

Console.WriteLine("Welcome to the best Quiz App ever :)");

Step 6: Looping Through Questions

Time to loop through each question and get the player's answers:

for (int i = 0; i < questions.Length; i++)
{
    // Code for Steps 7 to 10 goes here
}

Step 7: Displaying the Question

Display the question number and the actual question:

Console.WriteLine("Question " + (i + 1));
Console.WriteLine(questions[i]);

Step 8: Showing the Answers

Show the answer choices for the current question:

Console.WriteLine(answers[i]);

Step 9: Taking Player's Answer

Get the player's answer and store it in a variable:

Console.Write("Please enter your answer ('A', 'B', or 'C'): ");
string playerAnswer = Console.ReadLine();

Step 10: Checking Answers

Check if the player's answer is correct and update their score:

if (string.Equals(playerAnswer, "A", StringComparison.OrdinalIgnoreCase) && correctAnswers[i] == 0)
{
    playerScore++;
}
else if (string.Equals(playerAnswer, "B", StringComparison.OrdinalIgnoreCase) && correctAnswers[i] == 1)
{
    playerScore++;
}
else if (string.Equals(playerAnswer, "C", StringComparison.OrdinalIgnoreCase) && correctAnswers[i] == 2)
{
    playerScore++;
}

Step 11: Wrapping Up the Quiz

Let the player know the quiz is completed:

Console.WriteLine("Quiz Completed!");

Step 12: Revealing the Score

Finally, reveal the player's score:

Console.WriteLine("Your score is: " + playerScore + "/" + questions.Length);

Full Code:

class QuizProgram
{
    static void Main()
    {
        // Create Questions
        string[] questions =
        {
            "What book holds the record for the fastest selling book in history?",
            "How old was Queen Elizabeth II when she was first crowned the Queen of England?",
            "Which blood type is a universal donor?"
        };

        // Create Answers
        string[] answers =
        {
            "A. The Hunger Games \nB. Harry Potter and the Dealthy Hallows \nC. To Kill A Mockingbird",
            "A. 27 \nB. 24 \nC. 31",
            "A. O negative \nB. B Positive \nC. AB"
        };

        int[] correctAnswers = { 1, 1, 0 };
        int playerScore = 0;

        Console.WriteLine("Welcome to the best Quiz App ever :)");
        for (int i=0; i < questions.Length; i++)
        {
            Console.WriteLine("Question " + (i + 1));
            Console.WriteLine(questions[i]);
            Console.WriteLine(answers[i]);
            Console.Write("Please enter your answer ('A','B', or 'C'): ");
            string playerAnswer = Console.ReadLine();

            // Validating Answers
            if(string.Equals(playerAnswer, "A", StringComparison.OrdinalIgnoreCase) && correctAnswers[i] == 0)
            {
                playerScore++;
            }
            else if(string.Equals(playerAnswer, "B", StringComparison.OrdinalIgnoreCase) && correctAnswers[i] == 1)
            {
                playerScore++;
            }
            else if(string.Equals(playerAnswer, "C", StringComparison.OrdinalIgnoreCase) && correctAnswers[i] == 2)
            {
                playerScore++;
            }
        }

        // Print score out to user
        Console.WriteLine("Quiz Completed!");
        Console.WriteLine("Your score is: " + playerScore + "/" + questions.Length);

    }
}
Previous
Previous

How To Code A Quiz App In Python