Leo
Leo

Reputation: 21

C# when building a multiple choice quiz, how do we count the correct / wrong answers and form a score base on the counts?

So i am going to have 4 questions and each question is worth 25 points and full point is 100. I am not sure how to count the answer base on my code and form score code. Thank you for helping me out. (I put 1 question to make the code shorter but in my VS there are 4 questions)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MCQuiz
{
    class Program
    {
        static void Main(string[] args)
        {


            string answer;

            Console.WriteLine("Q1-Every Statement in C# language must terminate with: ");
            Console.WriteLine(" \t a.  ; ");
            Console.WriteLine(" \t b.  , ");
            Console.WriteLine(" \t c. . ");
            Console.WriteLine(" \t d.  ? ");
            Console.WriteLine();


            Console.Write("Enter the letter (a, b, c, d) for correct Answer: ")

            answer = Console.ReadLine();

            if (answer == "a")
            {
                Console.WriteLine("Your Answer '{0}' is Correct.", answer);
            }
            else
            {
                Console.WriteLine("Your Answer '{0}' is Wrong.", answer);
            }
            Console.WriteLine();
            Console.WriteLine("****************************************");
           
        }
    }
}

Upvotes: 1

Views: 873

Answers (1)

ˈvɔlə
ˈvɔlə

Reputation: 10242

You could track the totalScore in an integer variable and add 25 each correct answer using the += operator, which is a shorthand for totalScore = totalScore + 25 in the following example.

class Program
{
    static void Main(string[] args)
    {
        int totalScore = 0;   // initiate variable
        string answer;

        Console.WriteLine("Q1-Every Statement in C# language must terminate with: ");
        Console.WriteLine(" \t a.  ; ");
        Console.WriteLine(" \t b.  , ");
        Console.WriteLine(" \t c. . ");
        Console.WriteLine(" \t d.  ? ");
        Console.WriteLine();


        Console.Write("Enter the letter (a, b, c, d) for correct Answer: ")

        answer = Console.ReadLine();

        if (answer == "a")
        {
            totalScore += 25;   // add score
            Console.WriteLine("Your Answer '{0}' is Correct.", answer);
        }
        else
        {
            Console.WriteLine("Your Answer '{0}' is Wrong.", answer);
        }
        Console.WriteLine();
        Console.WriteLine("****************************************");
        Console.WriteLine($"You scored {totalScore} points"); // output result
    }
}

Upvotes: 1

Related Questions