所以我需要用C#中的for循环计算数组中10个数字的平均值。 我不知道如何做到这一点,因为我是c#的新手。有人帮我出去吗?
这是我到目前为止的代码:
namespace opdracht2
{
class Program
{
static void Main(string[] args)
{
string[] array = new string[10] { "7", "8", "4", "6", "5,5", "7,5", "2", "3,3", "4.9", "8.9" };
for (int i = 0, i <= array.Count, i++)
{
total += array[i]
}
float average = total / array.Count
}
}
}
答案 0 :(得分:0)
using System;
class Program
{
static void Main(string[] args)
{
string[] cijfers = new string[10] { "7", "8", "4", "6", "5.5", "7.5", "2", "3.3", "4.9", "8.9" };
int i = 0;
double sum=0;
for (i = 0; i < 10; i++)
{
sum+=double.Parse(cijfers[i]);
Console.WriteLine("The sum is: {0}",sum);
}
Console.Write("The average is: {0}",sum/10);
}
}
答案 1 :(得分:-2)
此处的数组变量定义为您的数组。确保使用.Length
。这将返回一个整数,该整数是数组中元素的数量。平均值是所有元素之和除以元素数
float total = 0;
for(int i = 0, i < array.Length, i++) {
total += array[i]
}
float average = total/array.Length