-1

我需要找到并返回数组中所有值的平均值。我的代码的所有其他部分都可以工作和编译。有人可以帮我找到平均部分吗?它正在使用我在这门课上的 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;


    }

}
4

2 回答 2

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;
}

另请参阅如何测试数组是否包含某个值?

于 2013-10-30T20:09:55.733 回答
1
return getSum() / size;

这有效,但有一个警告。由于getSum()size是整数,因此您实际上得到了实际平均值的下限。如果您想要浮点除法(我认为您会这样做,因为返回类型是双精度!),请使用以下命令:

return (double)getSum() / size;
于 2013-10-30T20:10:40.330 回答