练习1:用函数来做,青歌赛打分:20位评委给一个选中手分,去掉一个最高分,去掉一个最低分求歌手的平均得分。
1 static void Main(string[] args) 2 {//用函数,青歌赛打分:20位评委给一个选中手分,去掉一个最高分,去掉一个最低分求歌手的平均得分。 3 4 int[] a = new int[20]; 5 int zuidazhi = 0, zuixiaozhi = 1000, zongfen = 0; 6 7 //打分 8 a =dafen (a ); 9 //计算 求最低分,最高分,平均分 返回一个值 10 zuidazhi =ZuiDaZhi (a ); 11 zuixiaozhi =ZuiXiaoZhi (a ); 12 zongfen =Zongfen (a ); 13 //输出 14 shuchu (a,zuidazhi ,zuixiaozhi ,zongfen ); 15 } 16 17 18 19 20 21 static void shuchu(int[] a,int zuidazhi ,int zuixiaozhi ,int zongfen ) 22 { 23 double pingjunfen = 1.0 * (zongfen - zuidazhi - zuixiaozhi) / (a.Length - 2); 24 Console.WriteLine("去掉一个最低分{0},去掉一个最高分{1},这位选手的总平均分为:{2}",zuixiaozhi ,zuidazhi ,pingjunfen ); 25 } 26 27 28 static int []dafen(int[] a ) //20个评委打的分数 //void 没有返回值的意思 (int[] a )意思是把数组a扔进来 29 { 30 for (int i=0;i<a.Length;i ++) //return null ;//是空的 31 { 32 Console .Write ("第{0}位评委打得分数为:",i +1); 33 a [i ]=Convert .ToInt32 (Console .ReadLine ()); 34 35 } 36 return a ; //把a的值送出来 37 38 } 39 40 static int ZuiDaZhi(int[] a ) //求最大值 41 { 42 int ZuiDaZhi =0; 43 for (int i=0;i<a.Length;i ++) 44 { 45 if (a [i]>ZuiDaZhi ) 46 { 47 ZuiDaZhi=a [i]; 48 } 49 } 50 return ZuiDaZhi ; 51 52 } 53 54 static int ZuiXiaoZhi(int[] a ) //求最小值 55 { 56 int ZuiXiaoZhi=1000; 57 for (int i=0;i<a.Length;i ++) 58 { 59 if (a [i]<ZuiXiaoZhi) 60 { 61 ZuiXiaoZhi=a [i ]; 62 } 63 } 64 return ZuiXiaoZhi; 65 } 66 67 68 static int Zongfen(int[] a) 69 { 70 int Zongfen = 0; 71 for (int i=0;i<a.Length;i ++) 72 { 73 Zongfen =Zongfen+a [i ]; 74 } 75 return Zongfen; 76 }
显示的结果: