|
-
Sep 10th, 2008, 06:27 AM
#1
Thread Starter
New Member
[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?
-
Sep 10th, 2008, 06:34 AM
#2
Re: Issue with Numbers
Have you 100% verified that yansw = anws?
-
Sep 10th, 2008, 07:14 AM
#3
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
-
Sep 10th, 2008, 07:36 AM
#4
Thread Starter
New Member
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.
-
Sep 10th, 2008, 08:31 AM
#5
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
-
Sep 10th, 2008, 08:32 AM
#6
Thread Starter
New Member
Re: Issue with Numbers
Ok thanks doc, that was the way I had it I've got it sorted now.
-
Sep 11th, 2008, 06:06 AM
#7
Re: Issue with Numbers
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|