【C#作业】学生成绩添加并排序,错误则抛出异常

版权声明:本人菜鸟一只,如文章有错误或您有高见,请不吝赐教 https://blog.csdn.net/qq_41138935/article/details/83860962
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp12
{
    public class Student : IComparable<Student>
    {
        public string StuNum;
        public string Name;
        public float Chinese;
        public float English;
        public float Math;
        public float Tot;

        public Student(string[] s)
        {
            StuNum = s[0];
            Name = s[1];
            Chinese = float.Parse(s[2]);
            English = float.Parse(s[3]);
            Math = float.Parse(s[4]);
            Tot = Chinese + English + Math;
        }
        public int CompareTo(Student other)
        {
            return (int)(other.Tot - this.Tot);
        }
        public override string ToString()
        {
            return StuNum + "\t" + Name + "\t" + Chinese + "\t" + English + "\t" + Math + "\t" + Tot;
        }
    }
    public class ChineseCompare : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            return y.Chinese.CompareTo(x.Chinese);
        }
    }
    class IDException : Exception
    {
        public IDException(string str)
        {
            Console.WriteLine(str);
        }
    }
    class Program
    {
        static List<Student> L1 = new List<Student>();
        static void Main(string[] args)
        {
            ////默认数据
            string[] s = new string[5];
            string str = "";
            StreamReader sr1 = new StreamReader("input.txt");
            while ((str = sr1.ReadLine()) != null)
            {
                s = str.Split('\t');
                L1.Add(new Student(s));
            }
            sr1.Close();

            while (true)
            {
                Console.WriteLine("------------------------------------");
                Console.WriteLine("1.显示所有学生");
                Console.WriteLine("2.对成绩进行排序");
                Console.WriteLine("3.添加学生");
                Console.WriteLine("4.退出");
                Console.Write("请选择要进行的操作:");
                int choice = 0;
                try
                {
                    choice = int.Parse(Console.ReadLine());
                }
                catch (Exception)
                {
                    Console.WriteLine("输入的序号错误,请重新输入!");
                    continue;
                }
                switch (choice)
                {
                    case 1:
                        Console.WriteLine("学号\t姓名\t语文\t英语\t数学\t总分");
                        foreach (Student stu in L1)
                        {
                            Console.WriteLine(stu);
                        }
                        break;
                    case 2:
                        char choice2=' ';
                        Console.WriteLine("请选择排序的标准:a.总分\tb.语文");
                        try
                        {
                            choice2 = Convert.ToChar(Console.ReadLine());
                        }catch (Exception)
                        {
                            Console.WriteLine("输入有误,请重新操作!");
                        }
                        if (choice2 == 'a')
                        {
                            L1.Sort();
                            Console.WriteLine("------------------------根据总分排序------------------------");
                            Console.WriteLine("学号\t姓名\t语文\t英语\t数学\t总分");

                        }
                        else if (choice2=='b')
                        {
                            L1.Sort(new ChineseCompare());
                            Console.WriteLine("------------------------根据语文成绩排序------------------------");
                            Console.WriteLine("学号\t姓名\t语文\t英语\t数学\t总分");
                        }
                        else
                        {
                            Console.WriteLine("输入有误,请重新操作!");
                            break;
                        }
                        foreach (Student stu in L1)
                        {
                            Console.WriteLine(stu);
                        }
                        break;
                    case 3:
                        Console.WriteLine("请按照\"学号\\t姓名\\t语文\\t英语\\t数学\"这样的格式输入需要添加的学生信息");
                        try
                        {
                            string strInput = Console.ReadLine();
                            Add(strInput);
                        }catch(IDException e)
                        {
                            Console.WriteLine(e.Message);
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("输入有误,请重新操作!");
                        }
                        break;
                    case 4:
                        System.Environment.Exit(0);break;
                    default:
                        Console.WriteLine("请输入对应的序号!");
                        break;
                }

            }
        }
        static void Add(string str)
        {
            string[] s = new string[5];
            //while ((str = Console.ReadLine()) != "#")
            //{
                s = str.Split('\t');
                Checked(s[0]);
                L1.Add(new Student(s));
           // }
        }
        static void Checked(string s)
        {
            for(int i = 0; i < L1.Count; i++)
            {
                if (L1[i].StuNum.Equals(s))
                {
                    throw new IDException("待添加的学生学号已存在!");
                }
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41138935/article/details/83860962