如何在C#中找到所有数组元素的平均值?
要找到平均值,首先设置一个数组L
int[] myArr = new int[10] {45,
23,
55,
15,
8,
4,
2,
5,
9,
14
};
现在找到总和并将其除以数组的长度以获得平均值。
int len = myArr.Length;int sum = 0;
int average = 0;
for (int i = 0; i < len; i++) {
sum += myArr[i];
average = sum / len;
}
让我们看看完整的代码 -
示例
using System;public class Program {
public static void Main() {
int[] myArr = new int[10] {
45,
23,
55,
15,
8,
4,
2,
5,
9,
14
};
int len = myArr.Length;
int sum = 0;
int average = 0;
for (int i = 0; i < len; i++) {
sum += myArr[i];
}
average = sum / len;
Console.WriteLine("Sum = " + sum);
Console.WriteLine("Average Of integer elements = " + average);
}
}
以上是 如何在C#中找到所有数组元素的平均值? 的全部内容, 来源链接: utcz.com/z/356304.html