using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ConsoleApplication1 { class Program { struct student { public int code;//pu修饰符blic public string name;//结构体成员 public int shengao; } static void Main(string[] args) { //结构体,用户自定义数据类型,变量组,可以一次性存多个数据变量,定义在main函数外边,class里边 //题目:定义一个学生的结构体,学号,姓名,身高,输入学生信息。按身高排序输出 ArrayList al = new ArrayList();//定义集合 //录入集合 for (int i = 1; i <= 10;i++ ) { student s = new student(); Console.Write("请输入学号"); s.code = int.Parse(Console.ReadLine()); Console.Write("请输入姓名"); s.name = Console.ReadLine(); Console.Write("请输入身高cm"); s.shengao = int.Parse(Console.ReadLine()); al.Add(s); } //冒泡排序 for (int i = 0; i < 9; i++) { for (int j = i + 1; j < 10; j++) { student si = (student)al[i]; student sj = (student)al[j]; if (si.shengao < sj.shengao) { student zhong = si; al[i] = al[j]; al[j] = zhong; } } } //遍历集合 foreach (student s in al) { Console.WriteLine(s.code + " " + s.name + " " + s.shengao); } Console.ReadLine(); } } }
时间: 2024-10-07 22:42:38