C#基础篇十小练习03

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace P03练习
{
    class Program
    {
        static void Main(string[] args)
        {
            Test03_01();
        }
 public static void Test03_01()
        {
            /* 请编写1个程序,该程序从控制台接收用户的输入班级的人数,然后分别从控制台接收每1个人的成绩.只要有1个的成绩不合法(不在0-100的范围或者输入的不是整数),就提示用户重新输入该名学生的成绩.当所有的学生的成绩输入完毕之后,请打印出全班平均分,然后再求出去掉1个最高分和去掉1个最低分后的平均分,然后将成绩由高到低的顺序打印出来.(25分)
             */
            Console.WriteLine("接收学员的个数:");
            int max = 0;//最高分
            int min = 0;//最低分
            int scoreTotal = 0;//总分数
            int count = int.Parse(Console.ReadLine());//总人数
            int[] arrScore = new int[count];//分数数组
 
            for (int i = 0; i < count; i++)
            {
                int score = Test03_01_01("请输入第" + (i + 1) + "学员的分数:");
                arrScore[i] = score;
                if (i == 0)
                {
                    max = score;
                    min = score;
                }
                scoreTotal += score;//加入总分数
                if (max < score) max = score;//判断是否为本次循环之内的最高分
                if (min > score) min = score;//判断是否为本次循环之内的最低分
            }
 
            float avg0 = (scoreTotal - max - min) * 1.0f / (count - 2);
 
            //1.0 排序
            List<int> list = arrScore.ToList();
            list.Sort();
 
            Console.WriteLine("总人数为{0},全班平均分为{1},去掉最高分后最低分后的平均分为{2}",
                count,
                scoreTotal * 1.0f / count,
                avg0);
 
            Console.WriteLine("由低到高显示分数:");
            foreach (int i in list)
            {
                Console.WriteLine(i);
            }
        }
        //提示用户输入一个 整型的数值,并且 必须在1-100之间
        public static int Test03_01_01(string strMsg)
        {
            int res = -1;
            while (true)
            {
                Console.WriteLine(strMsg);
                string str = Console.ReadLine();
                if (int.TryParse(str, out res))//将用户输入的字符串转成 数值
                {
                    if (res >= 0 && res <= 100)//判断数值知否为 0-100之间的数
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine("必须是0-100之间的数值!");
                    }
                }
                else
                {
                    Console.WriteLine("必须输入数值~~!");
                }
            }
            return res;
        }
}

猜你喜欢

转载自blog.csdn.net/adsl3373056/article/details/80769338