在 C# 控制台应用程序中返回数组的平均值

Returning an average for an array in C# Console App

我的程序的目标是获取 4 个输入(1-100 之间的数字输入)并使用 foreach 循环基于这些输入创建一个平均值。目前我的代码导致单行具有奇数平均值。我似乎忽略了什么,但无法弄清楚。

输入示例:"Enter a number between 1-100" 行总共重复四次,允许 4 个输入。

输出应该是一行 "The average is ##"。基于输入的数字。

我当前的输出是 "The average is ##" 重复 4 次显示收到的每个输入而不是平均值。

感谢任何帮助。

    class Program
{
    public static double avg(int[] arr)
    {
        double sum = 0;

        for (int i = 0; i < arr.Length; i++)
        {
            sum += arr[i];
        }
        return sum / arr.Length;
    }
    public static void Main(string[] args)
    {
        int SIZE = 4;
        string[] names = new string[SIZE];
        int[] score = new int[SIZE];
        double avg1 = 0;

        for (int i = 0; i < SIZE; i++)
        {
            Console.Write("Enter a score between 1 - 100.  ");
            score[i] = Convert.ToInt32(Console.ReadLine());
            while (true)
            {
                if (!(score[i] >= 1 && score[i] <= 100))
                {
                    Console.WriteLine("Invalid number entered.");
                    score[i] = Convert.ToInt32(Console.ReadLine());
                }
                else
                {
                    break;
                }
            }
            avg1 = avg(score);
        }
        foreach (double a in score)
        {
            Console.WriteLine("The average of the test scores is: " + Math.Round(a, 2));
        }
    }
}

}

我不确定你为什么要循环 Console.WriteLine 这就是你得到 4 次输出的原因。

你只需要使用:

Console.WriteLine("The average of the test scores is: " + Math.Round(avg1 , 2));

您的代码段应如下所示:

class Program
{
  public static double avg(int[] arr)
  {
    double sum = 0;

    for (int i = 0; i < arr.Length; i++)
    {
        sum += arr[i];
    }
    return sum / arr.Length;
}
public static void Main(string[] args)
{
    int SIZE = 4;
    string[] names = new string[SIZE];
    int[] score = new int[SIZE];
    double avg1 = 0;

    for (int i = 0; i < SIZE; i++)
    {
        Console.Write("Enter a score between 1 - 100.  ");
        score[i] = Convert.ToInt32(Console.ReadLine());
        while (true)
        {
            if (!(score[i] >= 1 && score[i] <= 100))
            {
                Console.WriteLine("Invalid number entered.");
                score[i] = Convert.ToInt32(Console.ReadLine());
            }
            else
            {
                break;
            }
        }

    }
    avg1 = avg(score);
    Console.WriteLine("The average of the test scores is: " + Math.Round(avg1, 2));
}

My current output is "The average is ##" being repeated 4 times displaying each input received instead of the average.

因为您的 avg1 = avg(score); 函数在 for 循环中。把它放在外面 for 循环。


代码重构建议:

Linq in C# provides .Sum() 计算数组中所有数字之和的函数。

要计算平均值,只需计算数组元素的总和并除以数组.Length

你可以单行完成,不需要自己写函数,

//Here you don't need separate function to calculate an average
var average = Math.Round((score.Sum() / score.Length),2);  
Console.WriteLine("The average of the test scores is: "+average);