Home > Back-end >  C# when building a multiple choice quiz, how do we count the correct / wrong answers and form a scor
C# when building a multiple choice quiz, how do we count the correct / wrong answers and form a scor

Time:12-04

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

CodePudding user response:

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

CodePudding user response:

  1. I already created a quick maker in console application.
  2. Take a look Quiz maker.
  3. See below code snippet for you question.
    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;
                string totalScore = 0;
    
                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.ReadKey();
    
                if (answer == "a")
                {
                    totalScore  = 25;
                    Console.WriteLine("Your Answer '{0}' is Correct.", answer);
                }
                else
                {
                    Console.WriteLine("Your Answer '{0}' is Wrong.", answer);
                }
                Console.WriteLine();
                Console.WriteLine("****************************************");
               
            }
        }
    }

  • Related