英文:
(Java) Generating random number for division program
问题
randomnum1 = 1 + (int)(Math.random()*9)
randomnum2 = 1 + (int)(Math.random()*9)
while randomnum1 >= randomnum2 or randomnum1 % randomnum2 != 0:
randomnum1 = 1 + (int)(Math.random()*9)
randomnum2 = 1 + (int)(Math.random()*9)
number1 = randomnum1
number2 = randomnum2
a = number1 // number2
# rest of program is below this
英文:
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
答案1
得分: 1
你的 while
条件检查除法结果是否为偶数 randomnum1/randomnum2 % 2 != 0
你应该替换为:
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 和 randomnum2 现在符合你的期望
int a = number1/number2;
因为 rand1 取模 rand2 == 0
意味着
它们没有余数
英文:
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
答案2
得分: 1
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) {
// 打印生成的第一组数字
System.out.println(randomnum1 + " " + randomnum2);
randomnum1 = 1 + (int)(Math.random() * 99);
randomnum2 = 1 + (int)(Math.random() * 99);
}
if (true) {
// 打印使语句为真的生成的数字
System.out.print("true: " + randomnum1 + " " + randomnum2);
}
}
英文:
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);
}
}
}
答案3
得分: 1
另一种方法是使用无限循环,并在满足条件时中断循环。
public class Main {
public static void main(String[] args) {
int randomNum1, randomNum2;
while (true) {// 无限循环
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;// 中断循环
}
}
}
}
一个示例运行:
8 / 2 = 4
英文:
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
答案4
得分: 1
一个更好的方法,无需使用任何循环,可以使用以下数学技巧:
public class SpecialRandom{
public void generate(){
int first = 2 + (int) (Math.random() * 99);
int second = 1 + (int) (Math.random() * 99);
// 确保第二个数始终较小
if (second>=first){ second%=first; }
if (second==0) { second++; }
first += second - (first%second); // 修正余数
System.out.println((first>second && first%second==0)
+ " : " +first+ " , " +second);
}
/*测试*/
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
英文:
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(); }
}
}
Result
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论