【问题标题】:Java Homework - Store counts to an arrayJava Homework - 将计数存储到数组中
【发布时间】:2014-05-08 13:03:01
【问题描述】:

我正在尝试编写一个程序,将随机数的计数存储到一维数组中。将数字计数分配给数组时,我遇到了困难。这是一个逻辑错误,它编译得很好。谢谢。

public class PP67 {
    public static void main(String[] args) {

        int zero =0, one=0, two=0, three=0, four=0, five=0, six=0, seven=0, eight=0, nine=0;
        int[] counts = new int[10];//create array
        int i;

        for(i = 0; i < 100; i++) {

            int rand = (int)(Math.random()*10);     //generate 100 random ints between 0-9

            if (rand == 1){                    //assign the random number counter to the array
                one++;
                counts[0] = one;


            } else if (rand == 2) {
                two++;
                counts[1] = two;


            } else if (rand == 3) {
                three++;
                counts[2] = three;


            } else if (rand == 4) {
                four++;
                counts[3] = four;


            } else if (rand == 5) {
                five++;
                counts[4] = five;


            } else if (rand == 6) {
                six++;
                counts[5] = six;


            } else if (rand == 7) {
                seven++;
                counts[6] = seven;


            } else if (rand == 8) {
                eight++;
                counts[7] = eight;


            } else if (rand == 9) {
                nine++;
                counts[8] = nine;


            } else if (rand == 0) {
                zero++;
                counts[9] = zero;

            }

        }
        System.out.println("The count for number 1 is: " + counts[0]); //need outside count loop
        System.out.println("The count for number 2 is: " + counts[1]);
        System.out.println("The count for number 3 is: " + counts[2]);
        System.out.println("The count for number 4 is: " + counts[3]);
        System.out.println("The count for number 5 is: " + counts[4]);
        System.out.println("The count for number 6 is: " + counts[5]);
        System.out.println("The count for number 7 is: " + counts[6]);
        System.out.println("The count for number 8 is: " + counts[7]);
        System.out.println("The count for number 9 is: " + counts[8]);
        System.out.println("The count for number 0 is: " + counts[9]);
    }
}

【问题讨论】:

  • 您遇到的具体问题是什么?
  • 在将其设置为数组之前尝试递增。
  • 对于 10 个阵列槽中的每一个,输出都显示为 0。它实际上并没有为我计算随机数。我需要程序计算随机数出现的次数。
  • 在预先增加并更改 rands 优先级之后,我能够运行它。谢谢大家。 @RUJordan n' Zou

标签: java arrays store


【解决方案1】:

1) 您不需要在变量一、二三等和数组计数中保留相同的信息。

2) 表达式int rand = (int)(Math.random()*10) 总是返回0。要在[0,9]中生成随机数,最好使用

Random r = new Random();
int rand = r.nextInt(10);

3) 您不需要所有这些if-else if 条件,您可以使用随机数作为数组计数的索引。

private final static int REP = 100;
private final static int MAX = 10;
public static void main(String[] args) {

    Random rand = new Random(System.currentTimeMillis());

    int[] counts = new int[MAX];
    for(int i=0; i<REP; i++)
    {
        int n = rand.nextInt(MAX);
        counts[n]++;
    }

    for(int i=0; i<MAX; i++)
    {
        System.out.printf("The count for number %d is: %d \n", i, counts[i] );
    }
}

【讨论】:

    【解决方案2】:

    问题出在这里:

    int rand = (int)Math.random()*10;
    

    作为higher precedence 的转换比乘法。

    由于Math.random[0, 1[ 中返回双精度值(如documentation 状态),将其转换为int 之后总是以0 结束。所以你总是在做0 * 10。因此rand 始终为 0。

    先将随机数乘以 10,然后将其转换为 int

    int rand = (int)(Math.random()*10);
    

    您也可以使用Random 类中的nextInt(int n) 方法。

    Random r = new Random();
    int rand = r.nextInt(10); //will generate a random number in [0, 9]
    

    【讨论】:

    • 不知道的人,[0, 1[的意思是“大于等于零小于一”。有些人可能更习惯[0, 1)
    猜你喜欢
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 2021-04-27
    相关资源
    最近更新 更多