循环java中Rnd Num的平均值

时间:2017-04-02 21:29:59

标签: java

当我在1到20之间生成3个随机整数时,我试图计算平均值。我必须将平均值舍入为int并重复10000次。当我测试我的程序重复2次时,我得到了输出:

Number is :18
Number is :14
Number is :2
sum is: 34
Average is: 11

Number is :13
Number is :3
Number is :6
sum is: 56
Average is: 18

但是,平均值应该是3个数字的平均值。这里,第二个循环将第一个循环的总和加到第二个循环之和,得到平均值错误(Sum:34 + 22 = 56)。我希望它是22,所以平均值为7.我注意到问题可能是这一行,sum + = n; ,但我不知道为每个循环添加3个数字以获得平均值的另一种方法。

package Histogram;

import java.util.Random;

public class RandomNumbers {
    public static void main(String[] args) {
        final int N = 2;
        Random rand = new Random();
        int n;
        int average;
        int sum = 0;

        for (int a = 0; a < N; a++) {
            for (int i = 0; i < 3; i++) {
                n = rand.nextInt(20) + 1;
                System.out.println("Number is :" + n);
                sum += n;
            }
            average = sum / 3;

        System.out.println("sum is: " + sum);
        System.out.println("Average is: " + average);

        }

    }
}

5 个答案:

答案 0 :(得分:1)

  

平均值应该是3个数字的平均值。在这里,   第二个循环将第一个循环的总和加到第二个循环的总和   使平均错误(总和:34 + 22 = 56)。我希望它是22所以   平均值为7

一种方法是在将sumsum打印到控制台后重置average的值。

示例:

int n;
int average;
int sum = 0;

for (int a = 0; a < N; a++) {
    for (int i = 0; i < 3; i++) {
        n = rand.nextInt(20) + 1;
        System.out.println("Number is :" + n);
        sum += n;
    }
    average = sum / 3;

    System.out.println("sum is: " + sum);
    System.out.println("Average is: " + average);
    sum = 0;
}

为了让生活更轻松,您可以在循环中使变量sum成为局部变量,以便在每次迭代时sum的值将重置为默认值:

示例:

int n;
int average = 0;
for (int a = 0; a < N; a++) {
     int sum = 0;
     for (int i = 0; i < 3; i++) {
        n = rand.nextInt(20) + 1;
        System.out.println("Number is :" + n);
        sum += n;
    }
    average = sum / 3;

    System.out.println("sum is: " + sum);
    System.out.println("Average is: " + average);
}

答案 1 :(得分:1)

第二个sum += n;循环内的for继续增加sum的值(即使循环中较旧的和值)。因此,每次在第一个循环开始时,您需要将sum的值重置为0.

for (int a = 0; a < N; a++) {
   sum = 0;
   //rest of the code

答案 2 :(得分:1)

您必须在第一次循环后清除sum变量:

//..
int sum;

for (int a = 0; a < N; a++) {
    sum = 0; // -------> Here it is necessary to be clean for the next iteration
    for (int i = 0; i < 3; i++) {
        n = rand.nextInt(20) + 1;
        System.out.println("Number is :" + n);
        sum += n;
    }
//...
}

答案 3 :(得分:0)

如果你想要总和我建议添加第二个iterationSum变量来加上3个数字。类似的东西:

for (int a = 0; a < N; a++) {
    int iterationSum = 0;
    for (int i = 0; i < 3; i++) {
        ...
        iterationSum += n;
    }
    average = iterationSum / 3;
    ...
}

如果你不关心总和,只需重置你在每次迭代中总和为0:

for (int a = 0; a < N; a++) {
    sum = 0;
    ...
}

答案 4 :(得分:0)

public static void main(String[] args) {
    final int N = 2;
    Random rand = new Random();
    int n;
    int average;
    int sum = 0;

    for (int a = 0; a < N; a++) {
        sum=0;// this makes the trick
        for (int i = 0; i < 3; i++) {
            n = rand.nextInt(20) + 1;
            System.out.println("Number is :" + n);
            sum += n;
        }
        average = sum / 3;

    System.out.println("sum is: " + sum);
    System.out.println("Average is: " + average);

    }

}