C# Tutorial – Math’s Quiz Game Version 2 in Windows Form Application and Visual Studio

In this tutorial we will be making a another math’s puzzle game. Instead of users entering the final sum of the numbers they will be guessing the symbol for the arithmetic calculations instead.  Find out which equation was used to get the result it shows on the app. This app only uses labels and keyboard event to work. Each time you answer successfully it will take you to the next question.

Lesson Objective –

  1. Create a math’s quiz game
  2. generate random numbers
  3. work with switch statements
  4. make the app respond to key presses

 

Video Tutorial

Download The Math Quiz Game Tutorial on GitHub

 

Full Source Code –

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Maths_Quiz_Version_2_MOO_ICT
{
    public partial class Form1 : Form
    {

        int numA;
        int numB;
        int total;

        Random rnd = new Random();
        string[] Maths = {"Add", "Subtract", "Multiply" };

        string secretAnswer;
        string userChoice;


        public Form1()
        {
            InitializeComponent();
            SetUpGame();
        }

        private void SetUpGame()
        {

            numA = rnd.Next(10, 20);
            numB = rnd.Next(0, 9);

            secretAnswer = Maths[rnd.Next(0, Maths.Length)];

            switch (secretAnswer)
            {
                case "Add":
                    total = numA + numB;
                    break;

                case "Subtract":
                    total = numA - numB;
                    break;

                case "Multiply":
                    total = numA * numB;
                    break;
            }

            lblNumA.Text = numA.ToString();
            lblNumB.Text = numB.ToString();
            lblTotal.Text = total.ToString();
            lblSymbol.Text = "?";
        }

        private void CheckAnswer()
        {

            if (userChoice == secretAnswer)
            {
                MessageBox.Show("Correct, Now try again!");
                SetUpGame();
            }
            else
            {
                MessageBox.Show("Incorrect, try again!");
                lblSymbol.Text = "?";
            }


        }

        private void KeyIsUp(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Q)
            {
                userChoice = "Add";
                lblSymbol.Text = "+";
                CheckAnswer();
            }
            if (e.KeyCode == Keys.W)
            {
                userChoice = "Subtract";
                lblSymbol.Text = "-";
                CheckAnswer();
            }
            if (e.KeyCode == Keys.E)
            {
                userChoice = "Multiply";
                lblSymbol.Text = "x";
                CheckAnswer();
            }
        }
    }
}

 




One response to “C# Tutorial – Math’s Quiz Game Version 2 in Windows Form Application and Visual Studio”

  1. ZackIken says:

    thanks dear