Stats Array找到平均值

时间:2013-10-30 20:06:02

标签: java

我需要找到并返回数组中所有值的平均值。我的代码的每个其他部分都可以工作和编译。有人可以帮我找到平均值吗? IT正在使用我在此类中使用的GUI

import java.awt.*;
import java.util.Random;  //for our random number generator




public class StatsArray {

//instance variables
private int size;  //how big is the array
private int[] stats; // an array of integers

//default constructor -overloaded method
StatsArray() {
    size = 10;
    stats = new int[size] ;  //instantiate the array called stats
}

public void display(Graphics g)
{
    int x = 50;  //coordinates for displaying
    int y = 40;

    //display the array with position number
    for(int i = 0; i < stats.length; i++)
    {
        g.drawString("Stats [" + i + "] = "+ stats[i],
         x, (y + 15 * i));
    }
}

public void fillArray()
 {
    /*fill the array with random numbers (int) in the range 0 - 100.*/
    Random random = new Random();

for (int i = 0; i < stats.length; i++)
    stats[i] = random.nextInt(100+1);

}

public int getSum() 
{
    //add up all the values in the array
       int sum = 0;// Variable to keep track of sum
  for (int i = 0; i < stats.length; i++) //For loop to cycle through the aray at each element
  {
     sum += stats[i]; // Add each element onto the total of sum
  }

    return sum;// Returns sum, which is the added total of all the elements
 }

public int getMax() 
{
    //return the maximum value in the array
        int max = stats[0];

        for (int i = 0; i < stats.length; i++)
        {
            if (max < stats[i])
            {
                max = stats[i];
            }
        }
        return max;
}

public int getMin()
{
    //return the minimum value in the array

        int min = stats[0];

        for (int i = 0; i < stats.length; i++)
        {
            if (min > stats[i])
            {
                min = stats[i];
            }
        }
        return min;
}


**public double getAverage() 
{
    //return the average
        return ;
}**

public int countValues(int lowRange, int highRange) 
{
    //count how many numbers are >= lowRange and <= highRange
    int count=0;
    for(int i=0;i<stats.length;i++)
    {
         if(stats[i]>=lowRange && stats[i]<=highRange)
         {
               count++;
         }
    }
    return count;
}

public boolean isValueFound(int someNumber) 
{
    //check to see if someNumber is in the array

    return true;
}


public void sortArray() 
{
    /*sort the array in ascending order - selection sort*/

    int tempValue;
    int min;

    for (int i = 0; i < (stats.length - 1); i++)
    {
        min = i;
        for (int j = (i + 1); j < (stats.length); j++)
        {
            if (stats[j] < stats[min])
            {
                min = j;
            }
        }
        tempValue = stats[min];
        stats[min] = stats[i];
        stats[i] = tempValue;


    }

}

2 个答案:

答案 0 :(得分:1)

getAverage更改为

 public double getAverage( )
 {
      return ( double )getSum( ) / ( double )stats.length;
 }

修改

回应你的评论:

再一次迭代你的数组。在迭代时,检查您是否遇到了所需的值。如果是,请返回true。

public boolean isValueFound( int someNumber )
{ 
    for( int i = 0; i < stats.length; i++ )
    {
        if( stats[ i ] == someNumber )
        {
            return true;
        }
    }

    return false;
}

另见How can I test if an array contains a certain value?

答案 1 :(得分:1)

return getSum() / size;

这有效,但有一点需要注意。由于getSum()size是整数,因此您实际上是实际平均值的最低点。如果你想要一个浮点除法(我认为你这样做,因为返回类型是双倍!),请使用以下内容:

return (double)getSum() / size;