2014-11-02 38 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;