What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake an addition and subtraction quiz for younger kids in Visual Basic 6
DescriptionThis example shows how to make an addition and subtraction quiz for younger kids in Visual Basic 6.
Keywordsmath, mathematics, flash cards, quiz, addition, subtraction, VB.NET
CategoriesUtilities, Puzzles and Games
When the program starts, its button says "Start."

When you click this button, the event handler checks variable m_QuestionNumber to see what question the user is viewing. If m_QuestionNumber is 0, then the quiz hasn't started so the program changes the button's caption to "Answer," resets the question nummer and answer counters, and calls ShowQuestion to display the first question.

If m_QuestionNumber > 0, then the program calls CheckAnswer to see if the user's current answer is correct.

Private Const NUM_QUESTIONS As Integer = 10
Private Const MIN_NUMBER As Integer = 0
Private Const MAX_NUMBER As Integer = 10
Private Const MAX_GUESSES As Integer = 2
Private m_QuestionNumber As Integer
Private m_NumRight As Integer
Private m_NumWrong As Integer
Private m_NumGuesses As Integer
Private m_Answer As Integer

Private Sub Form_Load()
    m_QuestionNumber = 0
End Sub

Private Sub cmdAnswer_Click()
    If m_QuestionNumber < 1 Then
        cmdAnswer.Caption = "Answer"
        m_QuestionNumber = 1
        m_NumRight = 0
        m_NumWrong = 0
    End If
End Sub
Subroutine ShowQuestion picks two random numbers between MIN_NUMBER and MAX_NUMBER. It then randomly pick an operator. This example uses only + and -. If the operator is -, the program ensures that num1 >= num2 so the result isn't a negative number (you could remove this restriction for more advanced students). The program also stores the result of the equation in m_Answer.

Next the code displays the equation (for example, "8 + 3") in lblQuestion. It sets m_NumGuesses to 0 and hides the lblTryAgain label. Finally it clears any previous answer and sets focus to the answer text box.

' Display a new question.
' This version picks from two operators: + and -.
Private Sub ShowQuestion()
Const NUM_OPERATORS As Integer = 2
Dim num1 As Integer
Dim num2 As Integer
Dim op As String
Dim temp As Integer

    num1 = Int(MIN_NUMBER + Rnd * (MAX_NUMBER - MIN_NUMBER _
        + 1))
    num2 = Int(MIN_NUMBER + Rnd * (MAX_NUMBER - MIN_NUMBER _
        + 1))

    ' Pick the operator.
    Select Case Int(1 + Rnd * NUM_OPERATORS)
        Case 1 ' +
            op = "+"
            m_Answer = num1 + num2
        Case 2 ' -
            op = "-"
            If num1 < num2 Then
                temp = num1
                num1 = num2
                num2 = temp
            End If
            m_Answer = num1 - num2
    End Select

    lblQuestion.Caption = _
        Format$(num1) & " " & op & " " & _
    m_NumGuesses = 0
    lblTryAgain.Visible = False

    Me.Caption = "MathCards " & _
        Format$(m_QuestionNumber) & _
        " of " & Format$(NUM_QUESTIONS)

    txtAnswer.Text = ""
End Sub
Subroutine CheckAnswer checks the user's answer. If the answer is correct, then the code increments the question and number correct counts. If m_QuestionNumber is greater than NUM_QUESTIONS, then the user has finished the quiz so the program displays statistics and resets the button's caption to "Start." In a real application, you might want to store the results in a file or database for later analysis by the teacher. You would also want to provide a more exciting success message to the user, possibly including sounds, animation, a certificate to print, and so forth.

If the user's answer is correct but they have not finished the quiz, the code calls ShowQuestion to display the next question.

If the user's answer is incorrect, the program increments m_NumGuesses. If m_NumGuesses is greater than MAX_GUESSES, then the program moves on to a new question. If m_NumGuesses <= MAX_GUESSES, the code displays the lblTryAgain label (which says "Try again!") and beeps.

' Check the user's answer.
Private Sub CheckAnswer()
    Dim guess As Integer = Val(txtAnswer.Text)
    txtAnswer.Text = ""

    If Math.Abs(guess - m_Answer) < 0.01 Then
        ' Right. Show another question.
        m_NumRight += 1
        m_QuestionNumber += 1
        If m_QuestionNumber > NUM_QUESTIONS Then
            ' We're done.
            lblQuestion.Text = ""
            txtAnswer.Text = ""
            Dim results As String = _
                "Results:" & vbCrLf & _
                "    Right: " & m_NumRight & vbCrLf & _
                "    Wrong: " & m_NumWrong & vbCrLf & _
                "  % Right: " & _
                FormatPercent(m_NumRight / (m_NumRight + _
            MessageBox.Show(results, "Good Job!")

            btnAnswer.Text = "Start"
            Me.Text = "MathCards"
            ' Show another question.
        End If
        ' Wrong. Give the user another chance.
        m_NumWrong += 1
        m_NumGuesses += 1
        If m_NumGuesses > MAX_GUESSES Then
            ' Show a new question.
            ' Let the user try again.
            lblTryAgain.Visible = True
        End If
    End If
End Sub
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.