结构体案例之学生成绩输入及排序
class Program { struct Student { public string name; public int age; public double Cshap; public double Database; public double Html; public double sum; } static void Main(string[] args) { Console.WriteLine("请输入学生的个数:"); int a = Convert.ToInt32(Console.ReadLine()); List<Student> list = new List<Student>(); for (int i = 0; i < a; i++) { Student s = new Student(); Console.WriteLine("请输入第{0}个学生的姓名:",i+1); s.name = Console.ReadLine(); Console.WriteLine("请输入第{0}个学生的年龄:", i + 1); s.age = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入第{0}个学生的Cshap成绩:", i + 1); s.Cshap = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("请输入第{0}个学生的数据库成绩:", i + 1); s.Database = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("请输入第{0}个学生的网页成绩:", i + 1); s.Html = Convert.ToDouble(Console.ReadLine()); s.sum = s.Cshap + s.Database + s.Html; list.Add(s); } //排序 for (int i = 0; i < list.Count-1; i++) { for (int j = 0; j < list.Count - 1 - i; j++) { if (list[j].sum <= list[j + 1].sum) { Student zj = list[j]; list[j] = list[j + 1]; list[j + 1] = zj; } } } //输出 Console.WriteLine("姓名\t年龄\tCshap\t数据库\t网页\t总分"); foreach (Student data in list) { Console.WriteLine(data.name+"\t"+data.age+"\t"+data.Cshap+"\t"+data.Database+"\t"+data.Html+"\t"+data.sum); } } }