1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace 方法002
8 {
9 class Program
10 {
11 //写一个方法 求一个数组中的最大值、最小值、总和、平均值
12 //将返回的4个值,放在一个数组中返回
13 static void Main(string[] args)
14 {
15 int[] numbs = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
16 int[] reslut = GetMaxMinSumAvg(numbs);
17 Console.WriteLine("输入数组的最大值是{0},最小值是{1},总和是{2},平均值是{3}",reslut[0], reslut[1], reslut[2], reslut[3]);
18 Console.ReadKey();
19 }
20 /// <summary>
21 /// 计算一个数组的最大值、最小值、总和、平均值
22 /// </summary>
23 /// <param name="numbers">要计算的数组</param>
24 /// <returns>最大值、最小值、总和、平均值形成的数组</returns>
25 public static int[] GetMaxMinSumAvg(int[] numbers)
26 {
27 // 假设res[0]是最大值,res[1]是最小值,res[0]是总和,res[0]是平均值
28 int[] res = new int[4];
29 res[0] = numbers[0];//Max
30 res[1] = numbers[0];//Min
31 res[2] = 0;
32 for (int i = 0; i < numbers.Length; i++)
33 {
34 //如果当前循环到的元素比我假定的最大值还大
35 if (res[0]<numbers[i])
36 {
37 //将循环到的元素赋值给res[0]
38 res[0] = numbers[i];
39 }
40 if (res[1] <numbers[i])
41 {
42 //将循环到的元素赋值给res[0]
43 res[1] = numbers[i];
44 }
45 res[2] += numbers[i];
46 }
47 res[3] = res[2] / numbers.Length;
48 return res;
49 }
50 }
51 }