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

在C#控制台应用程序中返回数组的平均值,c#,arrays,visual-studio,console-application,average,C#,Arrays,Visual Studio,Console Application,Average,我的程序的目标是获取4个输入(1-100之间的数字输入),并使用foreach循环基于这些输入创建平均值。目前,我的代码生成了具有奇数平均值的单独行。我似乎忽略了一些事情,但无法理解 输入示例:行“输入1-100之间的数字”总共重复四次,允许4次输入 输出应为一行“平均值为”。基于输入的数字 我当前的输出是“平均值是##”,重复4次,显示收到的每个输入,而不是平均值 感谢您的帮助 class Program { public static double avg(int[] arr

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

输入示例:行“输入1-100之间的数字”总共重复四次,允许4次输入

输出应为一行“平均值为”。基于输入的数字

我当前的输出是“平均值是##”,重复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));
        }
    }
}
类程序
{
公共静态双平均值(int[]arr)
{
双和=0;
对于(int i=0;i如果(!(score[i]>=1&&score[i]我不确定为什么要循环
控制台.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));
}
类程序
{
公共静态双平均值(int[]arr)
{
双和=0;
对于(int i=0;i=1和分数[i]
我当前的输出是“平均值为”,重复4次
显示接收到的每个输入,而不是平均值

因为你的
avg1=avg(score);
函数在for循环的内部。把它放在for循环的外部 循环


代码重构建议:

在C语言中,提供了计算数组中所有数字之和的函数

要计算平均值,只需计算数组元素之和并将其除以数组
.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);