Results 1 to 7 of 7

Thread: [RESOLVED] Issue with Numbers

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    15

    Resolved [RESOLVED] Issue with Numbers

    I've written some code that generates 2 numbers. Then it adds them together and stores that as a variable. Then prompts the question (number1 + number2)and stores the output of the inputbox as another variable.

    Code:
    num1 = Int(Rnd * 10) + 1
    num2 = Int(Rnd * 10) + 1
    anws = num1 + num2
    yansw = InputBox("What is " & num1 & " + " & num2 & "?", "Maths Question!")
    ^^I've declared all the variables, just havn't shown that^^

    Now this is where I am stuck.

    Using an If Statement
    Code:
    If yansw = anws Then
    *snip*
    
    Else 
    *snip*
    
    End If
    I know I'm adding the numbers right, but the program is not executing the code when the yansw = anws. Can anyone point anything out? Or make it better?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Issue with Numbers

    Have you 100% verified that yansw = anws?

  3. #3
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Issue with Numbers

    This code seems to work fine. Not sure why yours does not:
    Code:
    Dim num1 As Integer, num2 As Integer, anws As Integer
    
    Private Sub Form_Load()
    Randomize
    num1 = Int(Rnd * 10) + 1
    num2 = Int(Rnd * 10) + 1
    anws = num1 + num2
    yansw = InputBox("What is " & num1 & " + " & num2 & "?", "Maths Question!")
    If yansw = anws Then
        MsgBox "Correct!"
    Else
        MsgBox "Wrong."
    End If
    End Sub
    Doctor Ed

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    15

    Re: Issue with Numbers

    Ah, i had issues with code after the If statement... Sorry for the ka-huffle. But while your here. Can i ask VB to pick a number from the ones I give it?
    I thought
    Code:
    Int(Rnd(1,2,3,4,5,6,7,8))
    But it doesn't like that.

    Even better - Is there a command so VB generates only numbers divisible by (a) certain number(s)?
    Last edited by Jimmeh; Sep 10th, 2008 at 07:42 AM.

  5. #5
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Issue with Numbers

    Place a command button on your form, set a constant that limits the range of values, choose an integer divisor greater than 0:

    Code:
    Dim MyNum As Single
    Const Range = 100, Divisor = 4
    
    Private Sub Command1_Click()
    Do
        MyNum = Rnd * Range
        If MyNum / Divisor = Int(MyNum / Divisor) Then Exit Do
    Loop
    MsgBox MyNum & " is evenly divisible by " & Divisor
    End Sub
    
    Private Sub Form_Load()
    Randomize
    End Sub
    Doctor Ed

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    15

    Re: Issue with Numbers

    Ok thanks doc, that was the way I had it I've got it sorted now.

  7. #7
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Issue with Numbers

    Quote Originally Posted by Jimmeh
    Ok thanks doc, that was the way I had it I've got it sorted now.
    Good, better, best; never let it rest. The following code runs a million times faster than the looped code I posted above:
    Code:
    Const Range = 100, Divisor = 4
    
    Private Sub Command1_Click()
    MsgBox Int(Rnd * Range / Divisor) * Divisor & " is evenly divisible by " & Divisor
    End Sub
    
    Private Sub Form_Load()
    Randomize
    End Sub
    Doctor Ed

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