2014-11-02 96 views
0

我需要計算數組中輸入值的平均值。目前的計算是錯誤的,我不知道如何解決它。我已經嘗試過使用for語句,但它再次計算不正確,給了我錯誤的平均值。我會很感激你能給予的任何幫助。在不使用數組類的情況下在數組中尋找平均值

foreach (int scores in bowlerScores)// a for loop to continue to process the scores 
{//by moving through the array and adding each individual score 
averageScore += (scores/ SCORE_COUNT); 

編輯:剛纔看到INT成績,改變了一倍

+0

你會如何平均在一張紙上寫下的數字?忘記編程,你的算法是什麼? – 2014-11-02 02:58:34

回答

0

根據你的代碼,修改它以計算平均會是如下的方式:

int sum = 0; 
foreach (int scores in bowlerScores) 
{ 
    sum += scores; 
} 
double average = (double)sum/(double)SCORE_COUNT; 
0

的問題是因爲你每次都在進行整數除法。我會建議首先總結所有的分數,然後在最後執行浮點分割。

int sum = 0; 
foreach (int scores in bowlerScores) 
    sum += scores; 

float averageScore = (float)sum/SCORE_COUNT; 
相關問題