How do I get numbers to divide themselves with no remainders?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
How do I get numbers to divide themselves with no remainders?
Can you give us some examples of what exactly you mean?
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
This code does only dividing numbers with decimals.
double number4 = Math.floor(Math.random() * 13);
JOptionPane.showMessageDialog(null,number4,null,JO ptionPane.
INFORMATION_MESSAGE);
double number5 = Math.floor(Math.random() * 13);
JOptionPane.showMessageDialog(null,number5,null,JO ptionPane.
INFORMATION_MESSAGE);
String number7 = JOptionPane.showInputDialog("Enter result");
double number6 = Double.parseDouble(number7);
double result2 = Math.round(number4 / number5);
if (result2 == number6) {
JOptionPane.showMessageDialog(null,"correct",null,
JOptionPane.INFORMATION_MESSAGE);
}
else
JOptionPane.showMessageDialog(null,"wrong",null,
JOptionPane.INFORMATION_MESSAGE);
Why don't you just use the Random class?
Improving the world one idiot at a time!
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
How about starting with an exact, precise problem statement? (Do this before struggling with the ins and outs and variable types and other program considerations.)
For example, I might want to create a program that does something like this:
Generate a sequence of problems "manually" (using your brain, not a program) that shows how a run might go.Problem statement:
We are going to make a test for division of positive integers by positive integers where the quotient is an integer.
Now, for the quotient to be an integer, we know that the numerator must be an integer multiple of the denominator.
So...
The denominator will be an integer 2, 3, ..., 9
The numerator will be a multiple of this, where the multiplier is 2, 3, ..., 15
(We are assuming that problems with denominator equal to 1 or problems with numerator equal to denominator are so trivial that we don't want to waste time on them.)
For a text-based example it might go something like this:
I am going to present 5 fractions. You will be asked to calculate the quotient. The quotient will always be an integer. OK. Here goes: The fraction is 18 / 6. Enter the quotient : 3 You entered 3. The answer is 3 The fraction is 98 / 7. Enter the quotient : 12 You entered 12. The answer is 14 The fraction is 63 / 7. Enter the quotient : 9 You entered 9. The answer is 9 The fraction is 24 / 4. Enter the quotient : 6 You entered 6. The answer is 6 The fraction is 45 / 3. Enter the quotient : 15 You entered 15. The answer is 15 Of the 5 problems, the number of correct answers was 4.
Now, here's the Big Step:
Then think (yes think) about how you would create such a problem set. Really. Think about how you created the problem set. If you didn't create the set, go back and do it now. Really. Do it. You don't have to write out all of the verbiage; just create a few problems "manually."
If you can't be bothered to do this (if you think it's too much trouble), then goodbye. It was nice to meet you.
On the other hand if you are still with us...
Did you just "select" some "random" numbers (off the top of your head) and see whether their quotient was an integer? And then stumble around until you found some "random" integers that actually gave an integer quotient? Really?
Well, here's how it might have been done. I express the methodology in pseudo-code that could even be used to generate a program.
Decide on the number of problems that will be presented. Make a loop that does the following for that number of times: BEGIN LOOP Generate a random integer 2, 3, ..., 9. This is the denominator of the fraction. Generate a random integer 2, 3, ..., 15. This is the multiplier. Calculate the numerator: denominator times multiplier. (Note that since numerator equals denominator times multiplier, correct value of the quotient is equal to the value of the multiplier.) Prompt the user by printing out numerator and denominator, and asking the user to enter a value for the quotient. (If the user enters something other than an integer, or an integer that is less than or equal to zero, maybe let him/her try again.) Test to see whether the user's answer is correct and act accordingly. END LOOP Print a summary giving the user's score.
Does that make sense? If it doesn't, then create your own problem statement and see whether you can come up with a test set of problems and some pseudo-code that might lead to implementation.
Then, and only then, try to write some Java consistent with the pseudo-code.
Note that no floating point arithmetic is required for a problem involving integers.
Note, also, that there is a nextInt() method of the Random class that obtains an integer within a specified range from a random number generator.
Bottom line: Computer programs don't always do things the same way that humans do, but having a clear idea of what the heck the program must do and thinking about how such a thing might be done is recommended before actually sitting down with a text editor and writing program statements.
Cheers!
Z