Results 1 to 5 of 5

Thread: Math Practice

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    2

    Question Math Practice

    Hi Everyone
    I am trying to write a program using random number generator in a GUI environment. When the plus label is clicked it generates and display an additional problem. The random number generator generates numbers between 1 and 100.
    A textbox is used to enter the answer.
    When the "check" button is clicked it checks to see if the answer is right, if it is a message display "correct", if not a message displays "incorrect".
    I have most of the code. I used a function to get the sum of the two random numbers. I am trying to use a sub procedure to compare. When I do, everything comes up "correct". Here's what I have. If anyone one can help me with this I will appreciate it. It seems like my problem lies in the textbox somewhere when I am comparing my answers.

    Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    Dim answer As Integer
    answer = SumofNumber()

    Me.txtAnswer.Focus()
    Me.lblNum1.Text = num1
    Me.lblNum2.Text = num2
    Me.lblPlus.Text = plus
    Me.lblMessage.Text = ""
    End Sub
    Function SumofNumber() As Integer

    num1 = rand.Next(1, 101)
    num2 = rand.Next(1, 101)
    plus = ("+")
    equal = ("=")
    Return (num1 + num2)

    End Function


    Private Sub btnCheck_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCheck.Click
    Dim answer As Integer

    Me.txtAnswer.Text = result
    result = Val(Me.txtAnswer.Text)
    compareSum(answer)
    End Sub
    Sub compareSum(ByVal answer As Integer)

    If result <> answer Then
    lblMessage.Text = "Incorrect"
    End If
    If answer = result Then
    lblMessage.Text = "Correct"
    End If
    End Sub

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
    Me.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    rand = New Random
    End Sub

    thanks in advance
    computer_mom



    End Class

  2. #2
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Math Practice

    VB Code:
    1. Private Sub btnCheck_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCheck.Click
    2.         Dim answer As Integer 'This guy here never gets a value.
    3.  
    4.         [b]Me.txtAnswer.Text = result
    5.         result = Val(Me.txtAnswer.Text)[/b]' Here you set txtanswer.txt to result.. then result to txtanswer.text?
    6.         compareSum(answer) 'Yet you call the sub with it.
    7.     End Sub
    8.  
    9.     Sub compareSum(ByVal answer As Integer) 'This guy is then passed zero.
    10.  
    11.         If result <> answer Then 'So this guy will almost always fire.
    12.             lblMessage.Text = "Incorrect"
    13.         End If
    14.         If answer = result Then
    15.             lblMessage.Text = "Correct"
    16.         End If
    17.     End Sub

    There are a few problems I see right off the bat. (I made comments above). One thing you might consider doing is to just think it all through logically, then throw down the code to do what you want. Even writing it down can help sometimes. Also, this subsection of the forum is typically for language independent math logic and whatnot.. you should probably post this kind of stuff in the VB.NET forum

    Welcome aboard,

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  3. #3
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Math Practice

    It looks to me as though your big problem is variable declaration. Declaring answer as integer in the add button means that it will only retain its value whilst this sub is running. Similarly in your comparesum sub you use result to check against answer - but result has not been assigned a value within this sub. There are ways around this - first of all you don't need result at all - simply compare answer with the textbox. You may want to beware with Val, because this converts to the Double number format, which may differ slightly in a very small decimal place from the integer value. You can also declare variables outside of the subs - simply move it to a module or to the top of the class in which you are working. You might want to use the Friend declaration for answer in this case.

    Hope that gives you some ideas on where to look.

    zaza

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    2

    Smile Re: Math Practice

    thanks for the help. I figured my problem was in the declaring of my variables. I corrected it, and now it works perfectly. Thanks again.

    I am now trying to write a program dealing with guessing a number. I am suppose to use a random number generator to determine the number to be guessed. It should be between 1 and some upperbound. I need to use the Math. log function to determine the logarithm of a number.
    My question is exactly how would I use this Math.log function to determine the logarithm. I need to get an understanding of this before I begin.
    Any information you all can give me to point me in the direction I need to go will be much appreciated.
    Thanks
    computer_mom44

  5. #5
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Math Practice

    Just don't forget to use [VBCODE] [/VBCODE] tags around your VB code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width