2013-10-30 133 views
-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; 


    } 

} 
+0

通過'stats'陣去,添加所有值後,再除以陣列長度 – iluxa

回答

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?

+0

這個作品非常感謝!對於我沒有注意到的下一部分我需要做的是:public boolean isValueFound(int someNumber)我將如何執行此部分? – Trigonometreh

+0

再次感謝ssell!我的GUI現在工作完美! – Trigonometreh

+0

沒問題。不要忘記接受答案,以便問題得到有效解決。 – ssell

1
return getSum()/size; 

這工作,但有一個警告。由於getSum()size是整數,所以實際上你獲得了實際平均值的底線。如果你想要一個浮點除法(!我認爲你這樣做,返回類型爲雙),使用以下命令:

return (double)getSum()/size; 
+0

是的,你是正確的感謝指出! – Trigonometreh