从键盘接收4名学生的信息(学号、姓名、成绩)并按照成绩由高到低排列,用对象数组做。新手求指导

解决方案 »

  1.   

    比如输入“0001,张三,90”,然后回车,Console.ReadLine 会读取整个字符串,你把它放到一个string里面,然后对他进行 split,分割成string数组。自定义类,接收这三个参数。自定义类实现 ICompareable 接口,在 Compare 方法里面写排序规则。然后用 List<自定义类> Sort 方法进行排序。
      

  2.   

    class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Score { get; set; }
    }void Main()
    {
        List<Student> students = new List<Student>();
        for (int i = 1; i <= 4; i++)
        {
            Console.WriteLine("Please input the student name:");
            string name = Console.ReadLine();
            Console.WriteLine("Please input {0}'s ID:", Name);
            int id = int.Parse(Console.ReadLine());
            Console.WriteLine("Please input {0}'s Score:", Name);
            int score = int.Parse(Console.ReadLine());
            students.Add(new Student() { Name = name, ID = id, Score = score });
        }
        foreach (Student student in students.OrderByDescending(x => x.Score))
        {
            Console.WriteLine(student.Name);
        }
    }