Skip to main content
removed language tag from the title
Source Link
t3chb0t
  • 44.7k
  • 9
  • 85
  • 191

Simple Math Quiz Game in C#

Simple Math Quiz Game in C#

Simple Math Quiz Game in C#

Simple Math Quiz Game

Rollback to Revision 2
Source Link
BCdotWEB
  • 11.4k
  • 2
  • 28
  • 45

Updated: Simple Math Quiz Game in C#

  1. It is a simple math quiz game

    It is a simple math quiz game
  2. player gets 1 point in every correct answer

    player gets 1 point in every correct answer
  3. the game will be over when the player gives the incorrect answer

    the game will be over when the player gives the incorrect answer
  4. it displays total score the player earned, then the score resets.

    it displays total score the player earned, then the score resets.
  5. Is my program reasonable or is there anything i need to improve?

      static void Main(string[] args)
     {
     Console.Write("Please enter your Name: ");
     string userName = (Console.ReadLine());
    
     Console.WriteLine("Hello " + userName + ", Press ENTER to start the Math Quiz");
    
     Console.WriteLine();
     Console.ReadKey();
    
     Start:
    
     Random numberGenerator = new Random();
     int score = 0;
    
     while (true)
     {
         int num01 = numberGenerator.Next(1, 11);
         int num02 = numberGenerator.Next(1, 11);
    
         Console.WriteLine("What is " + num01 + " times " + num02 + " equal to?");
    
         int Answer = Convert.ToInt32(Console.ReadLine());
         int correctAnswer = num01 * num02;
    
         if (Answer == num01 * num02)
         {
             Console.ForegroundColor = ConsoleColor.Green;
             ++score;
             int responseIndex = numberGenerator.Next(1, 5);  
             switch (responseIndex)
             {
                 case 1:
                     Console.WriteLine("Great!");                      
                     Console.WriteLine("Your score: " + score);
                     break;
                 case 2:
                     Console.WriteLine("You nailed it!");
                     Console.WriteLine("Your score: " + score);
                     break;
                 case 3:
                     Console.WriteLine("You're correct!");
                     Console.WriteLine("Your score: " + score);
                     break;
                 default:
                     Console.WriteLine("Good Job " + userName + ", Keep it up!");
                     Console.WriteLine("Your score: " + score);
                     break;
             }
             Console.ResetColor();
             Console.ReadLine();
             Console.WriteLine();               
         }
         else
         {
             Console.ForegroundColor = ConsoleColor.Red;
             int responseIndex2 = numberGenerator.Next(1, 5);
             switch (responseIndex2)
             {
                 case 1:
                     Console.WriteLine("Are you even trying? The correct answer is " + correctAnswer);
                     break;
                 case 2:
                     Console.WriteLine("Ooops!!! The correct answer is " + correctAnswer);
                     break;
                 case 3:
                     Console.WriteLine("Oh, come on " + userName + " I know you can do better than that! The correct answer is " + correctAnswer);
                     break;
                 default:
                     Console.WriteLine("Sorry " + userName + ", that's incorrect, the correct answer is " + correctAnswer);
                     break;
             }
             Console.WriteLine(Environment.NewLine + "Game Over, Your score: " + score);
             Console.ResetColor();
             Console.WriteLine();
             Console.ReadLine();
             goto Start;
         }
     }
    
    Is my program reasonable or is there anything i need to improve?

Update**

I already made some revisions on my work, well, not much, haven't done the new block as recommend, haven't go through method yet. Back to my problem, I already eliminated the goto as advised, however, my scoring seems not working right, the condition should be every time the game resets the score will reset as well, in my case, when the game is over, the user was given 2 options to continue or to quit, say if the user choose to continue the game will reset, however the score wont revert back to 0. Appreciate if you could help me

    static void Main(string[] args)
    {
        Console.Write("Please enter your Name: ");
        string userName = (Console.ReadLine());

        Console.WriteLine("Hello " + userName + ", Press any keyENTER to start...." the Math Quiz");

        Console.WriteLine();
        Console.ReadKey();
        Console.Clear();

        // handles the gameStart:

        Random numberGenerator = new Random();
        int score = 0;
   
      var quit =while false;(true)
        {
            int num01 = numberGenerator.Next(1, 11);
            int num02 = numberGenerator.Next(1, 11);

            while (!quit)
            {
                Console.ClearWriteLine();
             "What is " var+ num01 = numberGenerator.Next(1, 11);
            + " times " var+ num02 = numberGenerator.Next(1, 11);
                var operation+ =" numberGenerator.Next(1,equal 5to?");
                string operatorString;
                int answer;

             int Answer = Convert.ToInt32(Console.WriteLineReadLine("\tMath Problem:"));
            int correctAnswer = num01 Console.WriteLine("\t-------------");* num02;

            if (Answer == num01 * num02)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                ++score;
                int responseIndex = numberGenerator.Next(1, 5);  
                switch (operationresponseIndex)
                {
                    case 1:
                        answerConsole.WriteLine("Great!"); = num01 + num02;                  
                        operatorStringConsole.WriteLine("Your =score: "+";" + score);
                        break;
                    case 2:
                        answer = num01Console.WriteLine("You -nailed num02;it!");
                        operatorStringConsole.WriteLine("Your =score: "-"; + score);
                        break;
                    case 3:
                        answer = num01 * num02;
                        operatorString =Console.WriteLine("You're "*";correct!");
                        break;
                    caseConsole.WriteLine("Your 4score:
                        answer = num01 / num02;
                        operatorString" =+ "/";score);
                        break;
                    default:
                        answerConsole.WriteLine("Good =Job 0;" + userName + ", Keep it up!");
                        operatorStringConsole.WriteLine("Your =score: "?"; + score);
                        break;
                }
                Console.WriteLine("\t{0} {1} {2}", num01, operatorString, num02);

                Console.Write("\nEnter your answer here (round down if necessary): ");
                var input = (Console.ReadLine());
                int inputAsInt;

                while (!int.TryParse(input, out inputAsInt))//Requires user to key in only integer values
                {
                    Console.Write("Answer must be an integer. Try again: ");
                    input = Console.ReadLine();
                }
                if (inputAsInt == answer)//reportSuccess
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    ++score;
                    int responseIndex = numberGenerator.Next(0, 4);
                    string[] reportSuccess = { "Great!", "You nailed it " + userName, "That's correct " + userName,
                        "Good Job " + userName + ", Keep it up!" };
                    Console.WriteLine(reportSuccess[responseIndex]);
                    Console.WriteLine("Your score: {0}", score);
                    Console.ResetColor();
                    Console.ReadLine();
                }
                Console.ResetColor();
                Console.ReadLine();
                Console.WriteLine();               
            }
            else//reportFailure
            {
                Console.ForegroundColor = ConsoleColor.Red;
                int responseIndex2 = numberGenerator.Next(1, 5);
                switch (responseIndex2)
                {
                    Console.ForegroundColor =case ConsoleColor.Red;1:
                    int responseIndex2 = numberGenerator Console.NextWriteLine(0, 4);
                    string[] reportFailure = {"Are you even trying? The correct answer is " + answer,correctAnswer);
                        "Are you even trying?break;
 The correct answer is " + answer,
              case 2:
         "Oh, come on " + userName + " I know you can do better than thatConsole.WriteLine("Ooops!!! The correct answer is " + answer,correctAnswer);
                        "Sorry " + userName + ", that's incorrect, the correct answer is " + answer };break;
                    Console.WriteLine(reportFailure[responseIndex2]);

                case 3:
   //Game Over
                    Console.WriteLine(Environment.NewLine + "Game Over"Oh, Yourcome score:on {0}", score);
  + userName + " I know you can do better than that! The correct answer is " + Console.ResetColor(correctAnswer);
 
                    //Continue or Quit

                    {break;
                        Console.Write("\nPress 'q' to quit, or any other key to continue... ");default:
                        if (Console.ReadKeyWriteLine().Key == ConsoleKey.Q) quit = true;         "Sorry " + userName + ", that's incorrect, the correct answer is " + correctAnswer);
    
                     }break;
                }
                Console.WriteLine(Environment.NewLine + "Game Over, Your score: " + score);
                Console.ResetColor();
                Console.WriteLine();
                Console.ReadLine();
                goto Start;
            }
        }
    }
}

Updated: Simple Math Quiz Game in C#

  1. It is a simple math quiz game

  2. player gets 1 point in every correct answer

  3. the game will be over when the player gives the incorrect answer

  4. it displays total score the player earned, then the score resets.

  5. Is my program reasonable or is there anything i need to improve?

      static void Main(string[] args)
     {
     Console.Write("Please enter your Name: ");
     string userName = (Console.ReadLine());
    
     Console.WriteLine("Hello " + userName + ", Press ENTER to start the Math Quiz");
    
     Console.WriteLine();
     Console.ReadKey();
    
     Start:
    
     Random numberGenerator = new Random();
     int score = 0;
    
     while (true)
     {
         int num01 = numberGenerator.Next(1, 11);
         int num02 = numberGenerator.Next(1, 11);
    
         Console.WriteLine("What is " + num01 + " times " + num02 + " equal to?");
    
         int Answer = Convert.ToInt32(Console.ReadLine());
         int correctAnswer = num01 * num02;
    
         if (Answer == num01 * num02)
         {
             Console.ForegroundColor = ConsoleColor.Green;
             ++score;
             int responseIndex = numberGenerator.Next(1, 5);  
             switch (responseIndex)
             {
                 case 1:
                     Console.WriteLine("Great!");                      
                     Console.WriteLine("Your score: " + score);
                     break;
                 case 2:
                     Console.WriteLine("You nailed it!");
                     Console.WriteLine("Your score: " + score);
                     break;
                 case 3:
                     Console.WriteLine("You're correct!");
                     Console.WriteLine("Your score: " + score);
                     break;
                 default:
                     Console.WriteLine("Good Job " + userName + ", Keep it up!");
                     Console.WriteLine("Your score: " + score);
                     break;
             }
             Console.ResetColor();
             Console.ReadLine();
             Console.WriteLine();               
         }
         else
         {
             Console.ForegroundColor = ConsoleColor.Red;
             int responseIndex2 = numberGenerator.Next(1, 5);
             switch (responseIndex2)
             {
                 case 1:
                     Console.WriteLine("Are you even trying? The correct answer is " + correctAnswer);
                     break;
                 case 2:
                     Console.WriteLine("Ooops!!! The correct answer is " + correctAnswer);
                     break;
                 case 3:
                     Console.WriteLine("Oh, come on " + userName + " I know you can do better than that! The correct answer is " + correctAnswer);
                     break;
                 default:
                     Console.WriteLine("Sorry " + userName + ", that's incorrect, the correct answer is " + correctAnswer);
                     break;
             }
             Console.WriteLine(Environment.NewLine + "Game Over, Your score: " + score);
             Console.ResetColor();
             Console.WriteLine();
             Console.ReadLine();
             goto Start;
         }
     }
    

Update**

I already made some revisions on my work, well, not much, haven't done the new block as recommend, haven't go through method yet. Back to my problem, I already eliminated the goto as advised, however, my scoring seems not working right, the condition should be every time the game resets the score will reset as well, in my case, when the game is over, the user was given 2 options to continue or to quit, say if the user choose to continue the game will reset, however the score wont revert back to 0. Appreciate if you could help me

    static void Main(string[] args)
    {
        Console.WriteLine("Hello " + userName + ", Press any key to start....");

        Console.WriteLine();
        Console.ReadKey();
        Console.Clear();

        // handles the game

        Random numberGenerator = new Random();
        int score = 0;
        var quit = false;
        {

            while (!quit)
            {
                Console.Clear();
                var num01 = numberGenerator.Next(1, 11);
                var num02 = numberGenerator.Next(1, 11);
                var operation = numberGenerator.Next(1, 5);
                string operatorString;
                int answer;

                Console.WriteLine("\tMath Problem:");
                Console.WriteLine("\t-------------");

                switch (operation)
                {
                    case 1:
                        answer = num01 + num02;
                        operatorString = "+";
                        break;
                    case 2:
                        answer = num01 - num02;
                        operatorString = "-";
                        break;
                    case 3:
                        answer = num01 * num02;
                        operatorString = "*";
                        break;
                    case 4:
                        answer = num01 / num02;
                        operatorString = "/";
                        break;
                    default:
                        answer = 0;
                        operatorString = "?";
                        break;
                }
                Console.WriteLine("\t{0} {1} {2}", num01, operatorString, num02);

                Console.Write("\nEnter your answer here (round down if necessary): ");
                var input = (Console.ReadLine());
                int inputAsInt;

                while (!int.TryParse(input, out inputAsInt))//Requires user to key in only integer values
                {
                    Console.Write("Answer must be an integer. Try again: ");
                    input = Console.ReadLine();
                }
                if (inputAsInt == answer)//reportSuccess
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    ++score;
                    int responseIndex = numberGenerator.Next(0, 4);
                    string[] reportSuccess = { "Great!", "You nailed it " + userName, "That's correct " + userName,
                        "Good Job " + userName + ", Keep it up!" };
                    Console.WriteLine(reportSuccess[responseIndex]);
                    Console.WriteLine("Your score: {0}", score);
                    Console.ResetColor();
                    Console.ReadLine();
                }
                else//reportFailure
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    int responseIndex2 = numberGenerator.Next(0, 4);
                    string[] reportFailure = {"Are you even trying? The correct answer is " + answer,
                        "Are you even trying? The correct answer is " + answer,
                        "Oh, come on " + userName + " I know you can do better than that! The correct answer is " + answer,
                        "Sorry " + userName + ", that's incorrect, the correct answer is " + answer };
                    Console.WriteLine(reportFailure[responseIndex2]);

                    //Game Over
                    Console.WriteLine(Environment.NewLine + "Game Over, Your score: {0}", score);
                    Console.ResetColor();
 
                    //Continue or Quit

                    {
                        Console.Write("\nPress 'q' to quit, or any other key to continue... ");
                        if (Console.ReadKey().Key == ConsoleKey.Q) quit = true;                           
                     }
                }
            }
        }
    }
}

Simple Math Quiz Game in C#

  1. It is a simple math quiz game
  2. player gets 1 point in every correct answer
  3. the game will be over when the player gives the incorrect answer
  4. it displays total score the player earned, then the score resets.
  5. Is my program reasonable or is there anything i need to improve?
    static void Main(string[] args)
    {
        Console.Write("Please enter your Name: ");
        string userName = (Console.ReadLine());

        Console.WriteLine("Hello " + userName + ", Press ENTER to start the Math Quiz");

        Console.WriteLine();
        Console.ReadKey();

        Start:

        Random numberGenerator = new Random();
        int score = 0;
 
        while (true)
        {
            int num01 = numberGenerator.Next(1, 11);
            int num02 = numberGenerator.Next(1, 11);

            Console.WriteLine("What is " + num01 + " times " + num02 + " equal to?");

            int Answer = Convert.ToInt32(Console.ReadLine());
            int correctAnswer = num01 * num02;

            if (Answer == num01 * num02)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                ++score;
                int responseIndex = numberGenerator.Next(1, 5);  
                switch (responseIndex)
                {
                    case 1:
                        Console.WriteLine("Great!");                      
                        Console.WriteLine("Your score: " + score);
                        break;
                    case 2:
                        Console.WriteLine("You nailed it!");
                        Console.WriteLine("Your score: " + score);
                        break;
                    case 3:
                        Console.WriteLine("You're correct!");
                        Console.WriteLine("Your score: " + score);
                        break;
                    default:
                        Console.WriteLine("Good Job " + userName + ", Keep it up!");
                        Console.WriteLine("Your score: " + score);
                        break;
                }
                Console.ResetColor();
                Console.ReadLine();
                Console.WriteLine();               
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                int responseIndex2 = numberGenerator.Next(1, 5);
                switch (responseIndex2)
                {
                    case 1:
                        Console.WriteLine("Are you even trying? The correct answer is " + correctAnswer);
                        break;
                    case 2:
                        Console.WriteLine("Ooops!!! The correct answer is " + correctAnswer);
                        break;
                    case 3:
                        Console.WriteLine("Oh, come on " + userName + " I know you can do better than that! The correct answer is " + correctAnswer);
                        break;
                    default:
                        Console.WriteLine("Sorry " + userName + ", that's incorrect, the correct answer is " + correctAnswer);
                        break;
                }
                Console.WriteLine(Environment.NewLine + "Game Over, Your score: " + score);
                Console.ResetColor();
                Console.WriteLine();
                Console.ReadLine();
                goto Start;
            }
        }
    }
added 3229 characters in body
Source Link
  1. It is a simple math quiz game

    It is a simple math quiz game

  2. player gets 1 point in every correct answer

    player gets 1 point in every correct answer

  3. the game will be over when the player gives the incorrect answer

    the game will be over when the player gives the incorrect answer

  4. it displays total score the player earned, then the score resets.

    it displays total score the player earned, then the score resets.

  5. Is my program reasonable or is there anything i need to improve?

    Is my program reasonable or is there anything i need to improve?

      static void Main(string[] args)
     {
     Console.Write("Please enter your Name: ");
     string userName = (Console.ReadLine());
    
     Console.WriteLine("Hello " + userName + ", Press ENTER to start the Math Quiz");
    
     Console.WriteLine();
     Console.ReadKey();
    
     Start:
    
     Random numberGenerator = new Random();
     int score = 0;
    
     while (true)
     {
         int num01 = numberGenerator.Next(1, 11);
         int num02 = numberGenerator.Next(1, 11);
    
         Console.WriteLine("What is " + num01 + " times " + num02 + " equal to?");
    
         int Answer = Convert.ToInt32(Console.ReadLine());
         int correctAnswer = num01 * num02;
    
         if (Answer == num01 * num02)
         {
             Console.ForegroundColor = ConsoleColor.Green;
             ++score;
             int responseIndex = numberGenerator.Next(1, 5);  
             switch (responseIndex)
             {
                 case 1:
                     Console.WriteLine("Great!");                      
                     Console.WriteLine("Your score: " + score);
                     break;
                 case 2:
                     Console.WriteLine("You nailed it!");
                     Console.WriteLine("Your score: " + score);
                     break;
                 case 3:
                     Console.WriteLine("You're correct!");
                     Console.WriteLine("Your score: " + score);
                     break;
                 default:
                     Console.WriteLine("Good Job " + userName + ", Keep it up!");
                     Console.WriteLine("Your score: " + score);
                     break;
             }
             Console.ResetColor();
             Console.ReadLine();
             Console.WriteLine();               
         }
         else
         {
             Console.ForegroundColor = ConsoleColor.Red;
             int responseIndex2 = numberGenerator.Next(1, 5);
             switch (responseIndex2)
             {
                 case 1:
                     Console.WriteLine("Are you even trying? The correct answer is " + correctAnswer);
                     break;
                 case 2:
                     Console.WriteLine("Ooops!!! The correct answer is " + correctAnswer);
                     break;
                 case 3:
                     Console.WriteLine("Oh, come on " + userName + " I know you can do better than that! The correct answer is " + correctAnswer);
                     break;
                 default:
                     Console.WriteLine("Sorry " + userName + ", that's incorrect, the correct answer is " + correctAnswer);
                     break;
             }
             Console.WriteLine(Environment.NewLine + "Game Over, Your score: " + score);
             Console.ResetColor();
             Console.WriteLine();
             Console.ReadLine();
             goto Start;
         }
     }
    
  1. It is a simple math quiz game
  2. player gets 1 point in every correct answer
  3. the game will be over when the player gives the incorrect answer
  4. it displays total score the player earned, then the score resets.
  5. Is my program reasonable or is there anything i need to improve?
  1. It is a simple math quiz game

  2. player gets 1 point in every correct answer

  3. the game will be over when the player gives the incorrect answer

  4. it displays total score the player earned, then the score resets.

  5. Is my program reasonable or is there anything i need to improve?

      static void Main(string[] args)
     {
     Console.Write("Please enter your Name: ");
     string userName = (Console.ReadLine());
    
     Console.WriteLine("Hello " + userName + ", Press ENTER to start the Math Quiz");
    
     Console.WriteLine();
     Console.ReadKey();
    
     Start:
    
     Random numberGenerator = new Random();
     int score = 0;
    
     while (true)
     {
         int num01 = numberGenerator.Next(1, 11);
         int num02 = numberGenerator.Next(1, 11);
    
         Console.WriteLine("What is " + num01 + " times " + num02 + " equal to?");
    
         int Answer = Convert.ToInt32(Console.ReadLine());
         int correctAnswer = num01 * num02;
    
         if (Answer == num01 * num02)
         {
             Console.ForegroundColor = ConsoleColor.Green;
             ++score;
             int responseIndex = numberGenerator.Next(1, 5);  
             switch (responseIndex)
             {
                 case 1:
                     Console.WriteLine("Great!");                      
                     Console.WriteLine("Your score: " + score);
                     break;
                 case 2:
                     Console.WriteLine("You nailed it!");
                     Console.WriteLine("Your score: " + score);
                     break;
                 case 3:
                     Console.WriteLine("You're correct!");
                     Console.WriteLine("Your score: " + score);
                     break;
                 default:
                     Console.WriteLine("Good Job " + userName + ", Keep it up!");
                     Console.WriteLine("Your score: " + score);
                     break;
             }
             Console.ResetColor();
             Console.ReadLine();
             Console.WriteLine();               
         }
         else
         {
             Console.ForegroundColor = ConsoleColor.Red;
             int responseIndex2 = numberGenerator.Next(1, 5);
             switch (responseIndex2)
             {
                 case 1:
                     Console.WriteLine("Are you even trying? The correct answer is " + correctAnswer);
                     break;
                 case 2:
                     Console.WriteLine("Ooops!!! The correct answer is " + correctAnswer);
                     break;
                 case 3:
                     Console.WriteLine("Oh, come on " + userName + " I know you can do better than that! The correct answer is " + correctAnswer);
                     break;
                 default:
                     Console.WriteLine("Sorry " + userName + ", that's incorrect, the correct answer is " + correctAnswer);
                     break;
             }
             Console.WriteLine(Environment.NewLine + "Game Over, Your score: " + score);
             Console.ResetColor();
             Console.WriteLine();
             Console.ReadLine();
             goto Start;
         }
     }
    
added 1442 characters in body; edited title
Source Link
Loading
Tweeted twitter.com/StackCodeReview/status/1034003001105629184
Removed off-topic question
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Loading
Source Link
Loading