C#第1次试验(3)设计一个程序,输入10个数存入数组中,求最大值、最小值和平均值.

时间:2021-04-22 15:11:38
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1dishierzhou2
{
class Program
{
static double Min(double[] num)
{
double min = num[0];
for (int i = 0; i < 10; i++)
{
if (min > num[i])
{
min = num[i];
}
}
return min;

}
static double Max(double[] num)
{
double max = num[0];
for (int i = 0; i < 10; i++)
{
if (max < num[i])
{
max = num[i];
}
}
return max;

}
static double Average(double[] num)
{
double average = 0, sum = 0;
for (int i = 0; i < 10; i++)
{
sum = sum + num[i];
}
average = sum / 10;

return average;

}

static void Main(string[] args)
{
Console.WriteLine("输入10个数存入数组,并求其最大值、最小值和平均值");
double[] num = new double[10];
for (int i = 0; i < 10; i++)
{
Console.Write("请您输入第{0}个数:", i + 1);
num[i] = double.Parse(Console.ReadLine());
}
double min1 = Min(num);
Console.WriteLine("您输入10个数中最小值为:{0}",min1);
double max1 = Max(num);
Console.WriteLine("您输入10个数中最大值为:{0}",max1);
double average1 = Average(num);
Console.WriteLine("您输入10个数中平均值为:{0}",average1);

Console.ReadKey();
}


}


}


输出结果:

C#第1次试验(3)设计一个程序,输入10个数存入数组中,求最大值、最小值和平均值.

实验总结:

必须把方法体放在main函数前面!