how do i make sure num1 is larger then num2

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob

    how do i make sure num1 is larger then num2

    ok i have a program tah randomizes number adn signs (+,-). i made it
    so little kids can learn math easily and i wouldn't have to write each
    question out.

    when the plus sign is used, eveyrthing works fine because they just
    add the two numbers. when the computer chooses the minus sign,
    sometimes the first number is smaller then the second and the kids
    cant do it because they do not know negative numbers.

    so my question is, how do i make sure that the first number (num1) is
    always bigger then (num2)?

    i have a "next" button to ask for the next question, when the user
    presses it, the computer tells the user if its right or wrong and
    gives the next question.

    here is my code:

    Dim num1 As Integer 'first number
    Dim num2 As Integer 'second number
    Dim sign(2) As String 'the sign that will be used


    Private Sub Form_Load()

    sign(0) = "+"
    sign(1) = "-"

    Randomize
    'randomize the computer
    lblNum1.Caption = Int(20 * Rnd)
    num1 = lblNum1.Caption
    'randomize num1 and store value
    lblNum2.Caption = Int(20 * Rnd)
    num2 = lblNum2.Caption
    'randomize num2 and store value
    lblSign.Caption = sign(Int(2 * Rnd))
    'randomize sign

    End Sub

    Private Sub cmdNext_Click()

    If lblSign.Caption = "+" Then
    If Val(txtAnswer.T ext) = Val(num1 + num2) Then
    MsgBox ("Great Job")
    ElseIf Val(txtAnswer.T ext) <> Val(num1 + num2) Then
    MsgBox "Look at the numbers carefully next time!",
    vbExclamation
    End If
    ElseIf lblSign.Caption = "-" Then
    If Val(txtAnswer.T ext) = Val(num1 - num2) Then
    MsgBox ("Great Job")
    ElseIf Val(txtAnswer.T ext) <> Val(num1 + num2) Then
    MsgBox "Look at the numbers carefully next time!",
    vbExclamation
    End If
    End If

    Randomize
    'randomize the computer
    lblNum1.Caption = Int(20 * Rnd)
    num1 = lblNum1.Caption
    'randomize num1 and store value
    lblNum2.Caption = Int(20 * Rnd)
    num2 = lblNum2.Caption
    'randomize num2 and srote value
    lblSign.Caption = sign(Int(2 * Rnd))
    'randomize sign

    txtAnswer.Text = ""

    End Sub
  • Rick Rothstein

    #2
    Re: how do i make sure num1 is larger then num2

    Two quick points. First, only execute the Randomize statement **once**
    within your program (in the Form_Load event is fine). Although it won't
    matter for your present application, overuse of Randomize actually reduces
    the range of random numbers that are returned. Second, you have an unusual
    style of commenting... you are probably the only one I've ever seen who
    places the comment **after** the statement it applies to as opposed to
    before it.

    See my comment at the end of this section of your code....
    [color=blue]
    > Private Sub Form_Load()
    >
    > sign(0) = "+"
    > sign(1) = "-"
    >
    > Randomize
    > 'randomize the computer[/color]
    *************** *************** ************[color=blue]
    > lblNum1.Caption = Int(20 * Rnd)
    > num1 = lblNum1.Caption
    > 'randomize num1 and store value
    > lblNum2.Caption = Int(20 * Rnd)
    > num2 = lblNum2.Caption
    > 'randomize num2 and store value
    > lblSign.Caption = sign(Int(2 * Rnd))
    > 'randomize sign[/color]
    *************** *************** ************[color=blue]
    >
    > End Sub[/color]

    I'm going to suggest that you do this last part (between the lines of
    asterisks that I've added) of this just a little bit different that you
    presently are in order to pick up the flexibility you require. Instead of
    assigning you random value to the Label's Captions first and then using the
    Captions to assign the value to your num1 and num2 variables, I going to
    reverse that order and delay assigning the values to the Captions until
    after we check if the first number will be smaller than the second one. If
    it is smaller, we will reverse them. Try replacing the section I've outlined
    with asterisks with this (I'm using the same commenting style you presently
    use):

    num1 = Int(20 * Rnd)
    ' Randomize num1 and store value
    num2 = Int(20 * Rnd)
    ' Randomize num2 and srote value
    If num1 < num2 Then
    Temp = num1
    num1 = num2
    num2 = Temp
    End If
    ' The above If-Then block swaps the values in
    ' the two variables if num1 is less than num2

    lblNum1.Caption = num1

    ' Assign num1 to the Caption of lblNum1
    lblNum2.Caption = num2
    ' Assign num2 to the Caption of lblNum2
    lblSign.Caption = sign(Int(2 * Rnd))
    ' Randomize sign

    Of course, you will have to make this same change at the end of your
    cmdNext_Click code module also, given the way you structured your program.
    For future consideration, you might want to check out how Functions and Sub
    (also know as subroutines) work. They allow you to have one piece of code
    that can be called from multiple locations within your program without
    having to repeat all of the code over and over again like you did with the
    code in the above section..

    Rick - MVP


    Comment

    • Larry Serflaten

      #3
      Re: how do i make sure num1 is larger then num2

      "Bob" <coste2001@hotm ail.com> wrote in message[color=blue]
      > so my question is, how do i make sure that the first number (num1) is
      > always bigger then (num2)?[/color]


      Here is one way....

      HTH
      LFS

      Option Explicit

      Private Answer As Long

      Private Sub Form_Load()
      Randomize
      Show
      cmdNext = True
      End Sub

      Private Sub cmdGuess_Click( )
      If Val(txtAnswer) = Answer Then
      MsgBox "Correct"
      Else
      MsgBox "The correct answer was: " & CStr(Answer)
      End If
      cmdNext = True
      End Sub

      Private Sub cmdNext_Click()
      Dim num1, num2

      txtAnswer = ""
      If Rnd < 0.5 Then
      lblSign = "+"
      num1 = Int(Rnd * 20) + 1
      num2 = Int(Rnd * 20) + 1
      Answer = num1 + num2
      Else
      lblSign = "-"
      num1 = Int(Rnd * 15) + 5
      num2 = Int(Rnd * num1) + 1
      Answer = num1 - num2
      End If
      lblNum1 = num1
      lblNum2 = num2
      If txtAnswer.Visib le Then txtAnswer.SetFo cus
      End Sub

      Private Sub txtAnswer_KeyDo wn(KeyCode As Integer, Shift As Integer)
      If KeyCode = vbKeyReturn Then
      KeyCode = 0
      cmdGuess = True
      End If
      End Sub






      -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
      http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
      -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

      Comment

      • Bob

        #4
        Re: how do i make sure num1 is larger then num2

        "Larry Serflaten" <Abuse@SpamBust ers.com> wrote in message news:<4040ecc4_ 3@corp.newsgrou ps.com>...[color=blue]
        > "Bob" <coste2001@hotm ail.com> wrote in message[color=green]
        > > so my question is, how do i make sure that the first number (num1) is
        > > always bigger then (num2)?[/color]
        >
        >
        > Here is one way....
        >
        > HTH
        > LFS
        >
        > Option Explicit
        >
        > Private Answer As Long
        >
        > Private Sub Form_Load()
        > Randomize
        > Show
        > cmdNext = True
        > End Sub
        >
        > Private Sub cmdGuess_Click( )
        > If Val(txtAnswer) = Answer Then
        > MsgBox "Correct"
        > Else
        > MsgBox "The correct answer was: " & CStr(Answer)
        > End If
        > cmdNext = True
        > End Sub
        >
        > Private Sub cmdNext_Click()
        > Dim num1, num2
        >
        > txtAnswer = ""
        > If Rnd < 0.5 Then
        > lblSign = "+"
        > num1 = Int(Rnd * 20) + 1
        > num2 = Int(Rnd * 20) + 1
        > Answer = num1 + num2
        > Else
        > lblSign = "-"
        > num1 = Int(Rnd * 15) + 5
        > num2 = Int(Rnd * num1) + 1
        > Answer = num1 - num2
        > End If
        > lblNum1 = num1
        > lblNum2 = num2
        > If txtAnswer.Visib le Then txtAnswer.SetFo cus
        > End Sub
        >
        > Private Sub txtAnswer_KeyDo wn(KeyCode As Integer, Shift As Integer)
        > If KeyCode = vbKeyReturn Then
        > KeyCode = 0
        > cmdGuess = True
        > End If
        > End Sub
        >
        >
        >
        >
        >
        >
        > -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
        > http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
        > -----== Over 100,000 Newsgroups - 19 Different Servers! =-----[/color]

        can u explain me this code, cause i got no idea wut ur doing here adn
        y do u have option explicit at the top?

        ur program looks cool and efficient so plz someone explain wut every
        line does.

        Comment

        • Larry Serflaten

          #5
          Re: how do i make sure num1 is larger then num2

          "Bob" <coste2001@hotm ail.com> wrote[color=blue][color=green]
          > >
          > > Here is one way....[/color][/color]
          [color=blue]
          > can u explain me this code, cause i got no idea wut ur doing here adn
          > y do u have option explicit at the top?
          >
          > ur program looks cool and efficient so plz someone explain wut every
          > line does.[/color]

          If you see a command you don't understand, put the cursor on it and
          press F1 to bring up Help. Then read about it in Help, check out the
          examples, etc. There is nothing there that is complicated. The one
          command you may have difficulty with is:

          cmdNext = True

          Just think of it as the program's way of clicking on that button.
          You can replace it with:

          cmdNext_Click

          Which calls the click routine (as if it was just a regular routine)
          It would still work the same, try it and see....

          LFS




          -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
          http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
          -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

          Comment

          • Rick Rothstein

            #6
            Re: how do i make sure num1 is larger then num2

            > > can u explain me this code, cause i got no idea wut ur doing here adn[color=blue][color=green]
            > > y do u have option explicit at the top?
            > >
            > > ur program looks cool and efficient so plz someone explain wut every
            > > line does.[/color]
            >
            > If you see a command you don't understand, put the cursor on it and
            > press F1 to bring up Help. Then read about it in Help, check out the
            > examples, etc. There is nothing there that is complicated. The one
            > command you may have difficulty with is:
            >
            > cmdNext = True
            >
            > Just think of it as the program's way of clicking on that button.
            > You can replace it with:
            >
            > cmdNext_Click
            >
            > Which calls the click routine (as if it was just a regular routine)
            > It would still work the same, try it and see....[/color]

            Actually, I think you have done something in your coding which may have also
            confused Bob... you relied on the default properties of the controls being
            used. For Bob's benefit, here is your code with those properties explicitly
            included...

            Rick - MVP

            Option Explicit

            Private Answer As Long

            Private Sub Form_Load()
            Randomize
            Show
            cmdNext.Value = True
            End Sub

            Private Sub cmdGuess_Click( )
            If Val(txtAnswer.T ext) = Answer Then
            MsgBox "Correct"
            Else
            MsgBox "The correct answer was: " & CStr(Answer)
            End If
            cmdNext.Value = True
            End Sub

            Private Sub cmdNext_Click()
            Dim num1, num2
            txtAnswer.Text = ""
            If Rnd < 0.5 Then
            lblSign.Caption = "+"
            num1 = Int(Rnd * 20) + 1
            num2 = Int(Rnd * 20) + 1
            Answer = num1 + num2
            Else
            lblSign.Caption = "-"
            num1 = Int(Rnd * 15) + 5
            num2 = Int(Rnd * num1) + 1
            Answer = num1 - num2
            End If
            lblNum1.Caption = num1
            lblNum2.Caption = num2
            If txtAnswer.Visib le Then txtAnswer.SetFo cus
            End Sub

            Private Sub txtAnswer_KeyDo wn(KeyCode As Integer, Shift As Integer)
            If KeyCode = vbKeyReturn Then
            KeyCode = 0
            cmdGuess.Value = True
            End If
            End Sub


            Comment

            • Larry Serflaten

              #7
              Re: how do i make sure num1 is larger then num2

              "Rick Rothstein" <rickNOSPAMnews @NOSPAMcomcast. net> wrote[color=blue]
              > Actually, I think you have done something in your coding which may have also
              > confused Bob...[/color]

              You may be right, I just had a look at his first post and saw he qualified everything.
              I guess thats one of those cases where RAD does not necessarily mean good!

              Sorry for the confusion!
              LFS





              -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
              http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
              -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

              Comment

              • Bob

                #8
                Re: how do i make sure num1 is larger then num2

                thanks a lot guys, i understand it all better now.

                Comment

                Working...