int[] array = { 12, 33, 21, 34, 55, 6 };
for (int i = 0; i < array.Length-1; i++)
{
for (int j = 0; j < array.Length-i-1; j++)
{
if (array[j]>array[j+1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
//最大值
Console.WriteLine("最大值为:"+array[array.Length-1]);
//最小值
Console.WriteLine("最小值为:"+array[0]);
最小值
//Console.WriteLine(array.Min());
最大值
//Console.WriteLine(array.Max());
平均值
//Console.WriteLine(array.Average());
总和
//Console.WriteLine(array.Sum());
//总和
double sum=0;
for (int i = 0; i < array.Length; i++)
{
sum += array[i];
}
Console.Write("总和为:"+sum);
Console.WriteLine();
//平均值
double p =sum / (array.Length);
Console.Write("平均值为:" + p);
Console.WriteLine();
C#求出数组中的最大数、最小数、总和、平均值
最新推荐文章于 2022-09-02 11:56:03 发布