-
Random Array
Ok I'm trying to create a multiplier where you can press a command button and it Randomizes to numbers and multiplies it.
this is the code so far...anyone know whats wrong..? please help if you know.
Code:
Private Sub cmdrandom_Click()
Dim MAX As Integer
Dim MIN As Integer
For MAX = 1 To 200
For MIN = 1 To 200
If cmdrandom.CausesValidation = True Then
Dim x As Integer
Dim y As Integer
Next
x = txtcode.Text
y = TxtCode1.Text
x = MAX
y = MIN
txtdone.Text = y * x
End If
End Sub
it keeps saying "COMPILE ERROR:"
"NEXT WITHOUT FOR"
-
You have two For... statements, but only one Next statement. You need a Next for each For.
Also, your Next statement is within an If...Then block that's within one of the For... loops, which won't work. Your code should look like this:
Code:
For MAX = 1 To 200
For MIN = 1 To 200
If cmdRandom.CausesValidation Then
' Do something
End If
Next MIN
Next MAX
Finally, the code in the inner loop will execute 40,000 times (200 * 200). It's unlikely that cmdRandom's CausesValidation property will change inside the loop, so you should probably test it once before entering the loops, rather than testing it 40,000 times.
Code:
If cmdRandom.CausesValidation Then
For MAX = 1 To 200
For MIN = 1 To 200
' Do something
Next MIN
Next MAX
End If
Last edited by Phil Weber; 02-25-2005 at 01:26 AM.
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
 Originally Posted by Phil Weber
You have two For... statements, but only one Next statement. You need a Next for each For.
Also, your Next statement is within an If...Then block that's within one of the For... loops, which won't work. Your code should look like this:
Code:
For MAX = 1 To 200
For MIN = 1 To 200
If cmdRandom.CausesValidation Then
' Do something
End If
Next MIN
Next MAX
Finally, the code in the inner loop will execute 40,000 times (200 * 200). It's unlikely that cmdRandom's CausesValidation property will change inside the loop, so you should probably test it once before entering the loops, rather than testing it 40,000 times.
Code:
If cmdRandom.CausesValidation Then
For MAX = 1 To 200
For MIN = 1 To 200
' Do something
Next MIN
Next MAX
End If
That Worked Perfect thanks...But, How Do I make it choose any number out of the 1 to 200?
-
your code is really confusing, because before you set X and Y from two text boxes, then just after that you assign MIN and MAX to them. What's the point of using the text boxes if their values are not used?
To generate a random number between 1 and 200:
1+ fix(rnd * 199)
Marco
-
Yeah I just figured that i didnt need the 2 integers...Oh yeah thanks for that code..but i had to fix it to this: 1 To fix(rnd * 180). Thanks alot Mstraf and Phil!
-
one TO fix?!?!?!
Are you using a random number for your exit loop? I had the idea that you wanted to choose just a random number...
Marco
-
1 To fix(rnd * 180) is choosing random number for me...here try it yourself:
Private Sub CmdRandom_Click()
Dim MAX As Integer
Dim MIN As Integer
For MAX = 1 To Fix(Rnd * 180)
For MIN = 1 To 1 + Fix(Rnd * 180)
If CmdRandom.CausesValidation Then
txtcode.Text = MIN
TxtCode1.Text = MAX
txtdone.Text = MIN * MAX
End If
Next MIN
Next MAX
If CmdRandom.CausesValidation Then
For MAX = 1 To Fix(Rnd * 180)
For MIN = 1 To Fix(Rnd * 180)
Next MIN
Next MAX
End If
End Sub
-
Did you at least try to debug your code? Put a break at the first for statement, run and go step by step using F8....
The first two nested for statements loop a lot of times (set by two random numbers) but in the end in txtdone what you see is the results of only the last loop.
What about the completely useless two nested loops inside the last if statement, that don't do anything at all?
Sorry, but I am still trying to find out what you want to accomplish...
If you want to click in a button and multiply two random numbers, this is my code (not tested):
private sub command1_click()
txtcode.Text = 1 + fix(rnd * 200)
txtcode1.Text = 1 + fix(rnd * 200)
txtdone.text = txtcode.text * txtcode1.text
end sub
Marco
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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks