Exact division (whole)

Asked

Viewed 1,080 times

3

I need to make a division but that it always gives zero rest, exact division.
I think I’m sinning in condition.

Random Number;
int Rnumber1;
int Rnumber2;
RandomValor1 = (TextView) findViewById(R.id.TxtValor1);
RandomValor2 = (TextView) findViewById(R.id.TxtValor2);

Number = new Random();
Rnumber1 = Number.nextInt(20);
Rnumber2 = Number.nextInt(10);


while (Rnumber1/Rnumber2==0 ) {

    Rnumber1 = Number.nextInt(20);
    Rnumber2 = Number.nextInt(10);


}

RandomValor1.setText(Integer.toString(Rnumber1));
RandomValor2.setText(Integer.toString(Rnumber2));

1 answer

5


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:

  1. The divisor shall be a number between 1 and 10
  2. 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

  • while (Rnumber1 % Rnumber2 == 0 && Rnumber1>Rnumber2) { Rnumber1 = Number.nextInt(20); Rnumber2 = Number.nextInt(10); Randomvalor1.setText(Integer.toString(Rnumber1)); Randomvalor2.setText(Integer.toString(Rnumber2)); }

  • You’ve already made the change I indicated?

  • 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...

  • @seamusd "that operator should only be used for integers." That’s not truth

  • Change the conditions: while (Rnumber1 >= Rnumber2 && Rnumber1 % Rnumber2 != 0)

  • @ramaral in my design, it would only be possible for whole numbers. But if you’re saying. Abs. + 1 by the answer.

  • 1

    @seamusd It’s not me who says, it’s the java specifications.

  • 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?

  • // 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.

  • Yeah, that’s the idea. Troque Rnumber1 >= Rnumber2 for Rnumber1 < Rnumber2. I hadn’t noticed the condition was wrong.

  • 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?

  • I will edit the answer with a "better".

  • 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.

  • You’re right, I’m sorry, the division’s missing.

  • 1

    Solved... was not && and yes || ... now this round... Thanks for the help. Final result while ((Rnumber1 < Rnumber2) || (Rnumber1 % Rnumber2 != 0))

Show 11 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.