【问题标题】:(Java) Generating random number for division program(Java) 为除法程序生成随机数
【发布时间】:2021-01-27 01:06:16
【问题描述】:

您好,我是编程新手,我正在尝试制作一个可以生成 2 个随机数的除法程序,条件是第一个数字必须大于第二个数字,并且没有余数。如果生成的数字不满足条件,它将继续生成,直到满足条件。谁能帮我解决我的错误?

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

【问题讨论】:

  • 一种更简单的方法是选择两个因子 x 和 y,然后将它们相乘得到 z = x*y。然后问乘积 z 和其中一个因子 x 的商是多少。正确答案是您选择的另一个因素,即 y。在这种情况下,您可以完全消除循环并获得三个满足您正在寻找的条件的数字。
  • 仅提及您在该领域的未来努力。作为一条建议,您希望通过使用无限循环来实现这一点的方式并非绝对有效。在现实生活中,这种实现永远不会被接受。

标签: java math


【解决方案1】:

您的while 条件检查除法结果是否为偶数randomnum1/randomnum2 % 2 != 0
你应该替换:

while (randomnum1 < randomnum2 && 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 and randomnum2 now match your expectations
int a = number1/number2;

作为rand1 modulo rand2 == 0 的意思是

他们不留余数

【讨论】:

  • 您好,我遇到了一个错误,因为它说必须初始化随机数。我如何在此处发布更多代码?我是stackoverflow的新手
  • @JohnDoe619 您应该在 while 循环之前分配它们,就像您在帖子中所做的那样。在循环之前用randomnum1 = 1 + (int)(Math.random()*9); randomnum2 = 1 + (int)(Math.random()*9); 初始化它们。
【解决方案2】:

另一种方法是使用无限循环并在满足条件时中断循环。

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
            }
        }
    }
}

示例运行:

8 / 2 = 4

【讨论】:

    【解决方案3】:
          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);
               }
           }
           
           
           }
    

    【讨论】:

    • randomnum1 != randomnum2 是不必要的,因为randomnum1&lt;randomnum2...
    • 您需要将&amp;&amp; 替换为||,并检查这两个数字是否相等——如果是,您需要再次生成它们...
    • @Avi 是的,第一个答案是正确的,但是您对 || 的看法是错误的,再读一遍问题,余数必须=0。
    • 如果余数不为0 第一个数不大于另一个,我们需要重新生成它们
    • 如果余数不是 0 - 我们也应该再次生成数字
    【解决方案4】:

    无需使用任何循环的更好方法是执行一些数学技巧,如下所示:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-12
      • 1970-01-01
      • 1970-01-01
      • 2014-03-21
      • 2014-06-02
      • 1970-01-01
      • 2015-11-25
      相关资源
      最近更新 更多