例题:青歌赛打分程序。去掉两个最高分,去掉两个最低分,再算平均分。主要学习数组和冒泡排序,什么情况下要定义数组

 Console.Write("请输入评委人数:");//输入人数

            int renshu = Convert.ToInt32(Console.ReadLine());//人数代入

            int[] fenshu = new int[renshu];//定义一个分数的数组,长度为人数

            if (renshu >= 5)//去掉连个最高分,去掉两个最低分,所以至少五个人

            {

                for (int i = 0; i < renshu; i++)

                {

                    Console.Write("请输入第" + (i + 1) + "个评委打的分数:");

                    fenshu[i] = Convert.ToInt32(Console.ReadLine());

                }

                for (int i = 0; i < renshu; i++)

                {

                    for (int j = i; j < renshu - 1; j++)//冒泡排序,按分数从高到低排列

                    {

                        if (fenshu[i] < fenshu[j + 1])

                        {  

                           int zhong = 0;

                            zhong = fenshu[i];

                            fenshu[i] = fenshu[j + 1];

                            fenshu[j + 1] = zhong;

                        }

                    }

                }

                int he = 0;//计算总分,初始值为0

                for (int i = 0; i < renshu; i++)

                {

                    he += fenshu[i];//循环计算总分

                }

                double pjf = 0;//初始值为0

                pjf = (he - fenshu[0] - fenshu[1] - fenshu[renshu - 1] - fenshu[renshu - 2]) / (renshu - 4);                 //计算平均分。fenshu[renshu - 1] , fenshu[renshu - 2]),这两个分别代表最低分,和倒数第二个最低分,可能不好理解。在上一步冒泡循环中,已经将分数排好。renshu代表下标的的长度,减一就是下标的最后一个位置,就是最低分

                Console.WriteLine("去掉两个最高分" + fenshu[0] + "," + fenshu[1] + ",去掉两个最低分" + fenshu[renshu - 1] + "," + fenshu[renshu - 2] +                     "选手最后得分为" + pjf);

            }

            else

            {

                Console.WriteLine("请输入至少五个人数");

            }

            Console.ReadLine();

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。