class Program { struct student { public string name; public int code; public int age; public double fenshu; } static void Main(string[] args) { ArrayList al = new ArrayList(); //定义一个新的集合 Console.Write("请输入人数:"); int renshu = Convert.ToInt32(Console .ReadLine ()); for (int i = 0; i <renshu; i++) { student r = new student(); Console.Write("请输入第"+(i+1)+"个人的名字:"); r.name = Console.ReadLine(); Console.Write("请输入第" + (i + 1) + "个人的学号:"); r.code = Convert.ToInt32(Console .ReadLine ()); Console.Write("请输入第" + (i + 1) + "个人的年龄:"); r.age = Convert.ToInt32(Console .ReadLine ()); Console.Write("请输入第" + (i + 1) + "个人的分数:"); r.fenshu = Convert.ToDouble(Console .ReadLine ()); al.Add(r); //把r的数据都放在al这个集合里 } ; for (int i = 0; i < renshu ; i++) { for (int j = i; j < renshu-1; j++) if (((student)al[i]).fenshu<((student )al[j+1]).fenshu) //从student 结构体的集合里调出分数进行比较 { student zhong; zhong = (student )al[i]; al[i] = al[j + 1]; al[j+ 1] = zhong; } } Console .WriteLine ("排序后学生顺序:"); for (int i = 0; i < renshu; i++) { Console.WriteLine("名字\t" + ((student)al[i]).name + "学号\t" + ((student)al[i]).code + "年龄\t" + ((student)al[i]).age + "分数\t" + ((student)al[i]).fenshu ); } Console.ReadLine(); } } }
时间: 2024-10-13 00:44:36