[英](Java) Generating random number for division program
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.嗨,我是编程新手,我正在尝试制作一个可以生成 2 个随机数的除法程序,条件是第一个数字必须大于第二个数字,并且它们没有余数。 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
Your while
condition checks that the division result is even randomnum1/randomnum2 % 2 != 0
您的
while
条件检查除法结果是否偶数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由于
rand1 modulo rand2 == 0
意味着
they give no remainder
他们没有余数
public static void main(String[] args) {
int randomnum1=1 + (int)(Math.random()*99);
int randomnum2=1 + (int)(Math.random()*99);
while(randomnum1 % randomnum2 != 0 || randomnum1==randomnum2) {
//prints first numbers generated
System.out.println(randomnum1+" "+randomnum2);
randomnum1=1 + (int)(Math.random()*99);
randomnum2=1 + (int)(Math.random()*99);
}
if (true) {
//prints numbers generated that made the statement true
System.out.print("true :"+randomnum1+" "+randomnum2);
}
}
}
Another way of doing it can be by using an infinite loop and breaking the loop when the conditions are met.另一种方法是使用无限循环并在满足条件时中断循环。
public class Main {
public static void main(String[] args) {
int randomNum1, randomNum2;
while (true) {// An infinite loop
randomNum1 = 1 + (int) (Math.random() * 9);
randomNum2 = 1 + (int) (Math.random() * 9);
if (randomNum1 > randomNum2 && randomNum1 % randomNum2 == 0) {
System.out.println(randomNum1 + " / " + randomNum2 + " = " + (randomNum1 / randomNum2));
break;// Break the loop
}
}
}
}
A sample run:示例运行:
8 / 2 = 4
A better way, without having to use any loop, is to do some mathematical tricks as follows:一个更好的方法,无需使用任何循环,是做一些数学技巧如下:
public class SpecialRandom{
public void generate(){
int first = 2 + (int) (Math.random() * 99);
int second = 1 + (int) (Math.random() * 99);
// to guarantee the second is always smaller
if (second>=first){ second%=first; }
if (second==0) { second++; }
first += second - (first%second); //to correct remainder
System.out.println((first>second && first%second==0)
+ " : " +first+ " , " +second);
}
/*TESTING*/
public static void main(String []args){
SpecialRandom sr = new SpecialRandom();
for(int j=0; j<25; j++){ sr.generate(); }
}
}
true : 28 , 4
true : 64 , 32
true : 22 , 11
true : 18 , 3
true : 28 , 14
true : 18 , 6
true : 92 , 23
true : 96 , 6
true : 130 , 65
true : 28 , 14
true : 87 , 29
true : 87 , 29
true : 74 , 37
true : 112 , 56
true : 66 , 6
true : 10 , 1
true : 88 , 44
true : 68 , 34
true : 156 , 78
true : 22 , 11
true : 95 , 1
true : 86 , 43
true : 14 , 1
true : 82 , 41
true : 98 , 14
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.