[code]using System;using System.Text;namespace _2_3 {class Program {static void Main(string[] args) {Student[] students = new Student[10];bool success = true;while (success) {for (int i = 0; i < students.Length; i++) {Console.Write($\"请输入第{i + 1}个同学的名字:\");string name = Console.ReadLine();Console.Write($\"请输入第{i + 1}个同学的学号:\");int number = GetIntInput();Console.Write($\"请输入第{i + 1}个同学的语文成绩:\");int yuwen = GetIntInput();Console.Write($\"请输入第{i + 1}个同学的数学成绩:\");int shuxue = GetIntInput();Console.Write($\"请输入第{i + 1}个同学的英语成绩:\");int yingyu = GetIntInput();students[i] = new Student(name, number) {YuWen = yuwen,ShuXue = shuxue,YingYu = yingyu,};}Sort(students);Console.Write(\"是否继续?y/n:\");success = Console.ReadLine() == \"y\";}}/// <summary>/// 获得一个数字输入/// </summary>static int GetIntInput() {bool needReTry = true;while (needReTry) {string nStr = Console.ReadLine();if (int.TryParse(nStr, out int number)) {return number;}Console.Write(\"输入不是数字,请重试:\");}return 0;}static void Sort(Student[] students) {Sort(students, s => s.YuWen);Console.WriteLine($\"语文排序:{GetStr(students, s => s.YuWen)}\");Sort(students, s => s.ShuXue);Console.WriteLine($\"数学排序:{GetStr(students, s => s.ShuXue)}\");Sort(students, s => s.YingYu);Console.WriteLine($\"英语排序:{GetStr(students, s => s.YingYu)}\");Sort(students, s => s.GetAverage());Console.WriteLine($\"平均排序:{GetStr(students, s => s.GetAverage())}\");}static void Sort(Student[] students, Func<Student, int> GetValue) {for (int i = 0; i < students.Length - 1; i++) {for (int j = i + 1; j < students.Length; j++) {Student si = students[i];Student sj = students[j];if (GetValue(si) < GetValue(sj)) {students[i] = sj;students[j] = si;}}}}static string GetStr(Student[] students, Func<Student, int> GetValue) {StringBuilder builder = new StringBuilder(\" [ \");for (int i = 0; i < students.Length; i++) {if (i != 0) builder.Append(\"->\");builder.Append(students[i].Name);builder.Append(\":\");builder.Append(GetValue(students[i]));}builder.Append(\" ]\");return builder.ToString();}}}
student class
[code]/// <summary>/// 学生类/// </summary>public class Student {protected string name;public string Name => name;protected int number;public int Number => number;public int YuWen { get; set; }public int ShuXue { get; set; }public int YingYu { get; set; }public Student(string name,int number) {this.name = name;this.number = number;}public int GetAverage() {return (YuWen + ShuXue + YingYu) / 3;}}
运行结果: