Hi I’m new to programming and I’m trying to make a division program that can generate 2 random numbers and the condition is that the first number must be more than the second and they give no remainder. If the number generated does not meet the condition it keeps generating until the conditions are met. Can anyone help me fix my error?
randomnum1 = 1 + (int)(Math.random()*9); randomnum2 = 1 + (int)(Math.random()*9); while (randomnum1 < randomnum2 && randomnum1/randomnum2 % 2 != 0) { randomnum1 = 1 + (int)(Math.random()*9); randomnum2 = 1 + (int)(Math.random()*9); int number1 = randomnum1; int number2 = randomnum2; int a = number1/number2; //rest of program is below this
Advertisement
Answer
Your while
condition checks that the division result is even randomnum1/randomnum2 % 2 != 0
You should replace :
while (randomnum1 < randomnum2 && randomnum1/randomnum2 % 2 != 0) {
With
while (randomnum1 < randomnum2 || randomnum1 % randomnum2 != 0) { // while (!(randomnum1 >= randomnum2 && randomnum1 % randomnum2 == 0)) { randomnum1 = 1 + (int)(Math.random()*9); randomnum2 = 1 + (int)(Math.random()*9); } // randomnum1 and randomnum2 now match your expectations int a = number1/number2;
As rand1 modulo rand2 == 0
means that
they give no remainder