Forum Home
Press F1
 
Thread ID: 61507 2005-09-07 05:14:00 Visual Basic Help with Numbers :( vicjalan (8829) Press F1
Post ID Timestamp Content User
386634 2005-09-07 05:14:00 Hi All,

I'm currently working on a "simple" math application that will solve your basic operations using a range of numbers from 0-10. Pretty simple so far, but I have a problem with the division operation.

I want to be able to make the code recognize that the first number in the operation is divisible by the other.

For example, when a user is deviding 6/3 = The program validates that the answer is 2.

But my problem lies where I have my first number and second number being generated randomly, so when 6/5 comes out, the user cannot input a whole number.

I'm not sure if im making myself clear or not. but here is part of the code

============================
If radDivision.Checked = True Then
RandomLarge() 'A sub which generates the random number
Operator()' A sub where the Calculation and Verification is being processed
btnTestMe.Text = "Next Question" ' Changes the Text Property of the button.

===========================
Private Sub RandomLarge()
Dim obj As New System.Random
lblFirstNumber.Text = obj.Next(1, 11).ToString ' pick numbers
lblSecondNumber.Text = obj.Next(1, lblFirstNumber.Text).ToString ' picks numbers less than or equal to the first number
End Sub

===========================
Private Sub Operator()
If radDivision.Checked = True Then
lblops.Text = "÷"
End If
End Sub
===========================
If lblops.Text = "÷" Then
Calculate = CInt(lblFirstNumber.Text) / CInt(lblSecondNumber.Text)
End If

So basically as you can see, my operator sub determines from a radio button what a user wants to do, then based on the user's input it calculates and stores the answer to validate it with what the user inputs.

My RandomLarge() sub basically generates a random number but makes sure that the second number is not higher than the first number. But this does not guarantee that the answer will be a logical whole number. So what I want is for it generate only numbers that are divisible evenly into the other one.

Thanks in advance!
vicjalan (8829)
386635 2005-09-07 05:21:00 The quick and dirty way to generate the problem "C = A/B" would be: Don't randomly generate A and B. Generate B and C, make A=B*C and keep C "secret" .. it's the answer. ;) Graham L (2)
386636 2005-09-07 05:34:00 heh . . I appreciate your help Graham and that makes perfect sense in my braine hehe . . . but how do I make that into a code? :stare:

Sorry, but this is my very first time doing this .

Once again thanks . .
vicjalan (8829)
386637 2005-09-07 06:07:00 I hate BASIC. :groan:

It's just a matter of taking your time and thinking about it logically. I've always found the best place to write programmes is anywhere well away from a keyboard.

Here's some "pseudo code".

You've got the supplied random function. Use it to produce two numbers (call one "second" and one "result", say), setting the maximum allowed values to suit yourself. Multiply them, producing "first". Make "first" and "second" into strings if you have to, then present them to the user. (It might be an idea to make sure that "second" isn't zero. :cool: )
Graham L (2)
386638 2005-09-07 10:26:00 The *best* solution would allow you to change the range of numbers and still work . For instance, if the range becomes 20 through 50, the solution should still work . On that note, you'll want somewhere in the program to set the min/max range of numbers and then use them everywhere throughout the program . Arguably, the best bet is to create a Range object with a min/max property, however, even using variables would be an improvement . i . e .

instead of:
lblFirstNumber . Text = obj . Next(1, 11) . ToString ' pick numbers

declare a couple public constants at the start of your code (not in a sub):
Public Const minRange = 1
Public Const maxRange = 11

and then use them in your code . . . .

lblFirstNumber . Text = obj . Next( minRange, maxRange) . ToString
lblSecondNumber . Text = obj . Next(minRange, lblFirstNumber . Text) . ToString

See, now you can change the range in one place and it takes affect throughout your program .

Your solution is a little more complicated than multiplying the division answer by the smaller number, since it will cause your first number to be larger than 10 . Keep in mind also that each number is dividable by itself and 1 .
JudyK (8813)
386639 2005-09-08 07:55:00 Judy,

I appreciate your help and you have a good point about the object, I will try it and see what I get.

Graham I appreciate your help as well, but I was ticking at it all night and could not make that logic work :(

Once again, thanks guys.
vicjalan (8829)
1