简体   繁体   English

计算数组中随机整数的平均值

[英]calculating average of random integers in an array

I need to generate a specified number of random integers between any two values specified by the user (example, 12 numbers all between 10 and 20), and then calculate the average of the numbers. 我需要在用户指定的任意两个值之间生成指定数量的随机整数(例如,10到20之间的12个数字),然后计算数字的平均值。 The problem is if I ask for it to generate 10 numbers, it will only generate 9 (shown in output.) Also, if I enter a max range of 100 and min range of 90, the program will still generate #'s like 147, etc that are over the max range... did I mess up the random number generator? 问题是,如果我要求它生成10个数字,它将只生成9(显示在输出中。)此外,如果我输入最大范围100和最小范围90,程序仍将生成#的像147等超过最大范围......我搞乱了随机数生成器吗? Can someone help? 有人可以帮忙吗?

Here is the code I have so far: 这是我到目前为止的代码:

public class ArrayRandom
{
static Console c;           // The output console

public static void main (String[] args)
{
    c = new Console ();
    DecimalFormat y = new DecimalFormat ("###.##");

    c.println ("How many integers would you like to generate?");
    int n = c.readInt (); 
    c.println ("What is the maximum value for these numbers?");
    int max = c.readInt ();
    c.println ("What is the minimum value for these numbers?");
    int min = c.readInt ();

    int numbers[] = new int [n]; 
    int x;
    double sum = 0; 
    double average = 0; 

    //n = number of random integers generated
    for (x = 1 ; x <= n-1 ; x++) 
    {

        numbers [x] = (int) (max * Math.random () + min); 
    }

    for (x = 1 ; x <= n-1 ; x++) 
    {
        sum += numbers [x]; 
        average = sum / n-1); 

    }

    c.println ("The sum of the numbers is: " + sum); 
    c.println ("The average of the numbers is: " + y.format(average)); 

    c.println ("Here are all the numbers:"); 
    for (x = 1 ; x <= n-1 ; x++)  
{
        c.println (numbers [x]); //print all numbers in array
}


} // main method
} // ArrayRandom class

Java arrays are zero based. Java数组基于零。 Here you leave the first array element at its default value of 0 . 在这里,您将第一个数组元素保留为其默认值0 Replace 更换

for (x = 1 ; x <= n-1 ; x++) 

with

for (x = 0 ; x < n ; x++) 

Edit: To answer question (from now deleted comment) of why this does not produce values between min and max 编辑:回答问题(从现在删除的评论)为什么这不会产生最小值和最大值之间的值

max * Math.random () + min

Math.random generates double values between 0.0 and 1.0 . Math.random生成介于0.01.0之间的双精度值。 So for example, a min of 90 and max of 100 would generate numbers between and 90 and 190 (!). 因此,例如,最小值为90且最大值为100将产生介于90190之间的数字(!)。 To limit the values between the min and max you would need 要限制您需要的最小值和最大值之间的值

min + Math.random() * (max - min)
 ^    |_________________________|                          
 |                 |
90       value between 0 - 10     

Java arrays start indexing at 0. Also your loop is exiting one index short. Java数组开始索引为0.此外,您的循环退出一个索引short。 So, when n==6, your condition is then, "x <=5", and the loop exits. 因此,当n == 6时,您的条件是“x <= 5”,循环退出。 Try this: 尝试这个:

for ( x = 0; x < n; x++ {
   // stuff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM