【问题标题】:Need help using random numbers and modulos需要帮助使用随机数和模数
【发布时间】:2014-03-12 22:37:52
【问题描述】:

我正在尝试制作一个简单的程序,该程序将显示 20 个介于 1 和 100 之间的随机数,然后打印出哪些数字可以被 3 整除并等于 1%3 和 2%3。它似乎工作得很好,但我注意到它只适用于列表中的最后一个数字。在搜索我的数学时,我缺少什么来包含所有数字?提前感谢您为我提供的任何帮助!

import java.util.Random;


public class Lab5 {

public static void main(String[] args) {

    Random rnd = new Random();
    int repeat = 19;
    int n = 0;

    for(int i=0;i<=repeat;i++){
        n = rnd.nextInt(100)+1; 
        System.out.print(n+", ");

    }
    System.out.println();
    System.out.println("-------------------------------------");

    if(n % 3 == 0){
        System.out.println("Numbers divisible by three: "+n+(", "));

    }else{
        System.out.println("Numbers divisible by three: NONE");

    }
    System.out.println("-------------------------------------");
    if(n == 1 % 3){
        System.out.println("Numbers equivalent to one modulo three: "+n+(", "));

    }else{
        System.out.println("Numbers equivalent to one modulo three: NONE");
    }

    System.out.println("-------------------------------------");

    if(n == 2 % 3){
     System.out.println("Numbers equivalent to two modulo three: "+n+(", "));

    }else{
        System.out.println("Numbers equivalent to two modulo three: NONE");
    }

}

}

【问题讨论】:

  • 您正在执行 for 循环的 20 次迭代,每次都计算 n。然后,在 for 循环终止后,执行 if 块。您需要在 for 循环中执行数学运算。
  • 或者,将数字存储到数组(或集合)中,然后对数组中的值进行数学运算。

标签: java for-loop random division modulo


【解决方案1】:

它只打印最后一个数字,因为检查数字是否可整除等不在顶部的for 循环中。只需将其下方的所有代码复制并粘贴到您的 for 循环中,它就会按您的预期工作。

这里还有一个错误:if (n == 1 % 3),它是合法的,但会检查 n 是否等于 1 / 3 的余数。我认为这不是你想要实现的,所以像这样更正它: if (n % 3 == 1) 正如 Ypnypn 建议的那样。

【讨论】:

    【解决方案2】:

    您的n 在循环体之外声明,因此它的值将保持不变。但是,由于您在每次循环迭代中都覆盖了n,因此只有n 的最后一个值会保持不变,并会被程序的其他部分使用。

    正如 Ypnypn 所说,更正您对 modulo 的使用,并且正如 Arbiter 和 deanosaur 所建议的,将其余的程序逻辑移到 for loop

    【讨论】:

      【解决方案3】:

      模数的正确语法是n % 3 == 2。当前代码n == 2 % 3 表示n == 0,因为Java 中的操作顺序要求在相等之前计算模数。

      【讨论】:

        【解决方案4】:

        您将所有输出语句 (System.out.println()) 置于循环之外,因此它只输出最后一个值。

        移动你的输出语句,使它们在你的循环中:

        public static void main(String[] args) {
            Random rnd = new Random();
            int repeat = 19;
            int n = 0;
            int[] numbers = new int[3]; // To hold how many numbers have modulo 0, 1 or 2
            for(int i = 0; i <= repeat; i++) {
                n = rnd.nextInt(100)+1; 
                System.out.print(n+", ");
                if(n % 3 == 0) 
                    System.out.println("The number " + n + " is divisible by 3");
                else 
                    System.out.println("" + n + " modulo 3 = " + n % 3);
        
                numbers[n % 3]++;
            }
            System.out.println("Numbers divisible by 3: " + numbers[0]);
            System.out.println("Numbers with modulo 3 = 1: " + numbers[1]);
            System.out.println("Numbers with modulo 3 = 2: " + numbers[2]);
        }
        

        【讨论】:

          【解决方案5】:

          嗯..你没有在循环中计算任何东西,所以你的打印语句在你退出循环后工作 n 的最后一个值。尝试类似

          package com.example.modulo;
          import java.util.Random;
          public class Modulo {
          public static void main(String[] args) {
          
              Random rnd = new Random();
              int repeat = 19;
              int n = 0;
          
              int[] nMod = new int[3];
              nMod[0] = 0;
              nMod[1] = 0;
              nMod[2] = 0;
              for (int i = 0; i <= repeat; i++) {
                  n = rnd.nextInt(100) + 1;
                  nMod[n%3] = nMod[n%3] + 1;
                  System.out.print(n + " (" + n%3 + "), ");
              }
              System.out.println();
              System.out.println("-------------------------------------");
          
              System.out.println("Numbers divisible by three: " + nMod[0] + (", "));
              System.out.println("Numbers equivalent to one modulo three: " + nMod[1] + (", "));
              System.out.println("Numbers equivalent to two modulo three: " + nMod[2] + (", "));
          }
          }
          

          【讨论】:

          • 为什么要额外的模数包?
          • 这看起来不错,但你的数学有问题。对于可被 3 整除的数字,我得到诸如“2”之类的答案,但每行仍然只能得到一个结果。
          • 尝试修改循环中的打印语句以查看模数。看看这向您展示了什么
          猜你喜欢
          • 1970-01-01
          • 2014-11-30
          • 2015-01-25
          • 1970-01-01
          • 1970-01-01
          • 2014-12-21
          • 2015-07-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多