To get the rest of the division of two numbers use the module operator %
. See the following code:
while(valor1 % valor2 != 0)
The condition at the top means that while the rest of the valor1
for valor2
is different from zero, do something.
Adaptation to your code:
while (Rnumber1 % Rnumber2 != 0) {
Rnumber1 = Number.nextInt(20);
Rnumber2 = Number.nextInt(10);
}
To ensure that Rnumber1
is greater than or equal to Rnumber2
changes the condition like this:
while (Rnumber1 < Rnumber2 || Rnumber1 % Rnumber2 != 0) {
Rnumber1 = Number.nextInt(20);
Rnumber2 = Number.nextInt(10);
}
From what I understand you have the following requirements:
- The divisor shall be a number between 1 and 10
- The dividend shall be a number <= to 20 and >= to the divisor.
The method Number.nextInt(x)
generates numbers between 0
and x-1
, thus, to ensure the first requirement the code will be so:
Rnumber2 = Number.nextInt(10) + 1;
To ensure the following requirement we will do so:
R.number1 = Number.nextInt(21 - Rnumber2) + Rnumber2;
The modified code will thus be:
Random Number;
int Rnumber1;
int Rnumber2;
Number = new Random();
RandomValor1 = (TextView) findViewById(R.id.TxtValor1);
RandomValor2 = (TextView) findViewById(R.id.TxtValor2);
do {
Rnumber2 = Number.nextInt(10) + 1;
Rnumber1 = Number.nextInt(20 - Rnumber2 + 1) + Rnumber2;
}while (Rnumber1 % Rnumber2 != 0)
RandomValor1.setText(Integer.toString(Rnumber1));
RandomValor2.setText(Integer.toString(Rnumber2));
ramaral is the following, I’m doing a calculation game for child. I need the division operation to always be an exact division, type 2/2 6/3 etc... I am sinning on this... I need it to be displayed only if you have this criterion
– Alex Ferreira
while (Rnumber1 % Rnumber2 == 0 && Rnumber1>Rnumber2) { Rnumber1 = Number.nextInt(20); Rnumber2 = Number.nextInt(10); Randomvalor1.setText(Integer.toString(Rnumber1)); Randomvalor2.setText(Integer.toString(Rnumber2)); }
– Alex Ferreira
You’ve already made the change I indicated?
– ramaral
while (Rnumber1 % Rnumber2 != 0 && Rnumber1 >= Rnumber2) { Rnumber1 = Number.nextInt(20); Rnumber2 = Number.nextInt(10); Randomvalor1.setText(Integer.toString(Rnumber1)); Randomvalor2.setText(Integer.toString(Rnumber2)); } Not working...
– Alex Ferreira
@seamusd "that operator should only be used for integers." That’s not truth
– ramaral
Change the conditions:
while (Rnumber1 >= Rnumber2 && Rnumber1 % Rnumber2 != 0)
– ramaral
@ramaral in my design, it would only be possible for whole numbers. But if you’re saying. Abs. + 1 by the answer.
– viana
@seamusd It’s not me who says, it’s the java specifications.
– ramaral
to catching ugly... even with this condition, it generates a value less than the second. My doubt, with while in this condition that he is going to keep repeating until he gets the two correct conditions? only after it sequences in the code?
– Alex Ferreira
// Number = new Random(); Rnumber1 = Number.nextInt(20); Rnumber2 = Number.nextInt(10); while (Rnumber1 >= Rnumber2 && Rnumber1 % Rnumber2 != 0) { Rnumber1 = Number.nextInt(20); Rnumber2 = Number.nextInt(10); } Randomvalor1.setText(Integer.toString(Umberrn1)); Randomvalor2.setText(Integer.toString(Umberrn2);// that’s my button.
– Alex Ferreira
Yeah, that’s the idea.
Troque Rnumber1 >= Rnumber2
forRnumber1 < Rnumber2
. I hadn’t noticed the condition was wrong.– ramaral
Rnumber1 < Rnumber2 && Rnumber1 % Rnumber2 != 0 even so it continues generating operation type 13/4 0/5 I can’t understand... do I put a while inside the other type so?
– Alex Ferreira
I will edit the answer with a "better".
– ramaral
Ramaral, it’s not working. With this change he has made, he does not verify if it is an exact division... it has to have a validar of the results but of the wrong. Thanks for the help, I’ll try more here, qd resolve put the solution.
– Alex Ferreira
You’re right, I’m sorry, the division’s missing.
– ramaral
Solved... was not && and yes || ... now this round... Thanks for the help. Final result while ((Rnumber1 < Rnumber2) || (Rnumber1 % Rnumber2 != 0))
– Alex Ferreira