Hi Guys! I'm trying to create an addition game with an image to display as the given number (addends)
EX.
instead of just displaying texts like 1+1 = 2
I will be using images to display the given numbers and the answer.

I have created some codes here that was able to display the images randomly using an imagelist however the program isn't displaying the correct sum for the problem. Any advice and help is greatly appreciated.

Public Class Play

    Dim RandomGen As New Random
    Dim RandomNum1 As Integer
    Dim RandomNum2 As Integer
    Dim QuestionNumber As Integer = 0
    Dim Answer As Integer

    Dim NumGuesses As Integer = 5
    Const MaxGuesses As Integer = 2
    Const NumQuestions As Integer = 5

    Private Sub Play_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lblTryAgain.Visible = False

    End Sub

    Private Sub BtnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNext.Click
        RandomNum1 = RandomGen.Next(0, 9)
        RandomNum2 = RandomGen.Next(0, 9)

        lblQuestion1.Image = ImageList1.Images(RandomNum1)
        lblQuestion2.Image = ImageList1.Images(RandomNum2)

        If QuestionNumber < 1 Then
            BtnNext.Text = "Answer"
            QuestionNumber = 0
            ShowQuestion()
        Else
            CheckAnswer()
        End If

    End Sub

    Private Sub ShowQuestion()

        Dim addend1 As Integer = RandomGen.Next(9)
        Dim addend2 As Integer = RandomGen.Next(9)

        NumGuesses = 0
        lblTryAgain.Visible = False

        Answer = addend1 + addend2

        Me.Text = "Question " & _
            QuestionNumber.ToString() & _
            " of " & NumQuestions.ToString()

        txtAnswer.Text = ""
        txtAnswer.Focus()

        'Answer = RandomNum1 + RandomNum2
    End Sub

    Private Sub CheckAnswer()
        Dim guess As Integer = Val(txtAnswer.Text)
        txtAnswer.Text = ""
        txtAnswer.Focus()

        If Math.Abs(guess - Answer) < 0.01 Then
            QuestionNumber += 1

            If QuestionNumber > NumQuestions Then
                lblQuestion1.Text = ""
                lblQuestion2.Text = ""
                txtAnswer.Text = ""

            Else
                ' Show another question.
                ShowQuestion()

            End If

        Else
            If NumGuesses >= MaxGuesses Then
                ' Show a new question.
                ShowQuestion()

            End If
        End If



    End Sub

Just from a perusal of your code it looks like you're assigning random pictures to the labels, but you're generating two more random numbers to get the answer. Which stands to reason, that they probably won't match the images you've used.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.