用C#编写一段10个元素的均值算法。
以下是用C#编写的10个元素的均值算法:
using System;
class MainClass {
public static void Main (string[] args) {
double[] arr = new double[10] {2.5, 3.7, 1.6, 4.8, 6.2, 9.1, 5.3, 7.4, 2.9, 8.5};
double sum = 0;
for (int i = 0; i < arr.Length; i++) {
sum += arr[i];
}
double avg = sum / arr.Length;
Console.WriteLine("The average of the 10 elements is: " + avg);
}
}
这里我们定义了一个 double 类型的数组 arr,其中存储了10个元素。然后我们使用一个循环来遍历数组中的所有元素,并将它们的值累加到 sum 变量中。最后我们计算平均值 avg,并将其打印到控制台上。
原文地址: https://www.cveoy.top/t/topic/lX0 著作权归作者所有。请勿转载和采集!