C# 我如何返回整数数组的总和、平均值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12949907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:03:53  来源:igfitidea点击:

how can i return the sum , average of an int array

c#

提问by test test

I need to define two methods for returning the sumand averageof an int array the method defining is as follow:-

我需要定义用于返回两种方法sumaverageint数组定义方法如下的: -

public int Sum(params int[] customerssalary)
{

         // I tried the following but it fails   return customerssalary.sum();
}

Another question is how can I return the average of these int values ?.

另一个问题是如何返回这些 int 值的平均值?

采纳答案by musefan

This is the way you should be doing it, and I say this because you are clearly new to C# and should probably try to understand how some basic stuff works!

这是您应该这样做的方式,我之所以这么说是因为您显然是 C# 新手,并且可能应该尝试了解一些基本的东西是如何工作的!

public int Sum(params int[] customerssalary)
{
   int result = 0;

   for(int i = 0; i < customerssalary.Length; i++)
   {
      result += customerssalary[i];
   }

   return result;
}

with this Sumfunction, you can use this to calculate the average too...

使用此Sum功能,您也可以使用它来计算平均值...

public decimal Average(params int[] customerssalary)
{
   int sum = Sum(customerssalary);
   decimal result = (decimal)sum / customerssalary.Length;
   return result;
}

the reason for using a decimaltype in the second function is because the division can easily return a non-integer result

decimal在第二个函数中使用类型的原因是因为除法很容易返回非整数结果



Others have provided a Linq alternative which is what I would use myself anyway, but with Linq there is no point in having your own functions anyway. I have made the assumption that you have been asked to implement such functions as a task to demonstrate your understanding of C#, but I could be wrong.

其他人已经提供了一个 Linq 替代方案,无论如何我都会使用它,但是对于 Linq,无论如何拥有自己的函数是没有意义的。我假设你被要求实现这样的功能作为一项任务来展示你对 C# 的理解,但我可能是错的。

回答by L.B

customerssalary.Average();
customerssalary.Sum();

回答by Tim Schmelter

You have tried the wrong variable, intsis not the correct name of the argument.

您尝试了错误的变量,ints是不是参数的正确名称。

public int Sum(params int[] customerssalary)
{
    return customerssalary.Sum();
}

public double Avg(params int[] customerssalary)
{
    return customerssalary.Average();
}

But do you think that these methods are really needed?

但是你觉得这些方法真的有必要吗?

回答by Jon Skeet

Using ints.sum()has two problems:

使用ints.sum()有两个问题:

  • The variable is called customerssalary, not ints
  • C# is case sensitive - the method is called Sum(), not sum().
  • 变量被调用customerssalary,而不是ints
  • C# 区分大小写 - 方法被调用Sum(),而不是sum().

Additionally, you'll need a using directive of

此外,您还需要一个 using 指令

using System.Linq;

Once you've got the sum, you can just divide by the length of the array to get the average - you don't need to use Average()which will iterate over the array again.

获得总和后,您只需除以数组的长度即可获得平均值 - 您不需要使用Average()which 将再次遍历数组。

int sum = customerssalary.Sum();
int average = sum / customerssalary.Length;

or as a double:

或作为double

double average = ((double) sum) / customerssalary.Length;

回答by shakz

If you are using visual studio 2005 then

如果您使用的是 Visual Studio 2005 那么

public void sumAverageElements(int[] arr)
{
     int size =arr.Length;
     int sum = 0;
     int average = 0;

     for (int i = 0; i < size; i++)
     {
          sum += arr[i];
     }

     average = sum / size; // sum divided by total elements in array

     Console.WriteLine("The Sum Of Array Elements Is : " + sum);
     Console.WriteLine("The Average Of Array Elements Is : " + average);
}

回答by pimbrouwers

Though the answers above all are different flavors of correct, I'd like to offer the following solution, which includes a null check:

尽管以上所有答案都是不同的正确答案,但我想提供以下解决方案,其中包括空检查:

decimal sum = (customerssalary == null) ? 0 : customerssalary.Sum();
decimal avg = (customerssalary == null) ? 0 : customerssalary.Average();

回答by Hackbal Teamz

i refer so many results and modified my code its working

我参考了很多结果并修改了我的代码使其正常工作

foreach (var rate in rateing)
                {
                    sum += Convert.ToInt32(rate.Rate);
                }
                if(rateing.Count()!= 0)
                {
                    float avg = (float)sum / (float)rateing.Count();
                    saloonusers.Rate = avg;
                }
                else
                {
                    saloonusers.Rate = (float)0.0;
                }