在控制台程序中使用结构体、集合,完成下列要求
项目要求:
一、连续输入5个学生的信息,每个学生都有以下4个内容:
1、序号 - 根据输入的顺序自动生成,不需要手动填写,如输入第一个学生的序号是1,第二个是2,以此类推
2、学号 - 必填,如:S001,S002... 以此类推
3、姓名 - 必填
4、成绩 - 大于等于0,小于等于100
以上内容必须按照要求填写,请写好相应的验证,如果没填写正确,则让用户重复填写到正确为止
二、5个学生信息都输入完毕后,按照分数从高到低的顺序将学生信息展示出来
显示格式如下:
==============学生成绩展示=================
序号 学号 姓名 成绩
3 S003 张三 100
1 S001 李四 99
2 S002 王五 98
...
...
namespace ConsoleApplication6 { class Program { struct student { public int xuhao; public string xuehao; public string name; public double score; } static void Main(string[] args) { ArrayList al = new ArrayList(); student x = new student(); int biao = 0; Console.Write("请输入班级人数:"); int a = int.Parse(Console.ReadLine()); for (int i = 0; i < a; i++) { int biao1 = 0; x.xuhao = i+1; for (; ; ) { Console.Write("请输入第{0}个学生的学号:", i + 1); string xh = Console.ReadLine(); if (xh != "") { if (biao == 0) { x.xuehao = xh; biao1 = 1; break; } else { for (int k = 0; k < biao; k++) { if (((student)al[k]).xuehao == xh) { Console.WriteLine("学号输入重复,请重新输入!"); break; } else { if (k == biao - 1) { x.xuehao = xh; biao1 = 1; break; } else { continue; } } } } } else { Console.WriteLine("输入学号不能为空,请重新输入!"); } if (biao1 == 1) { break; } } for (; ; ) { Console.Write("请输入第{0}个学生的姓名:", i + 1); string name = Console.ReadLine(); if (name != "") { x.name = name; break; } else { Console.WriteLine("输入姓名不能为空,请重新输入!"); continue; } } Console.Write("请输入第{0}个学生的分数:", i+1 ); try { double m = double.Parse(Console.ReadLine()); if (m >= 0 && m <= 100) { x.score = m; } else { Console.WriteLine("成绩必须在0~100之间!"); } } catch { Console.WriteLine("成绩输入有误!"); } al.Add(x); } for (int i = 0; i < al.Count - 1; i++) { for (int j = i + 1; j < al.Count; j++) { student s1 = (student)al[i]; student s2 = (student)al[j]; if (s1.score < s2.score) { student zhong = (student)al[i]; al[i] = al[j]; al[j] = zhong; } } } Console.WriteLine("==============学生成绩展示================="); Console.WriteLine("序号" + " "+"学号" + " "+"姓名" + " "+"成绩" + " "); for (int i = 0; i < al.Count; i++) { Console.WriteLine(((student)al[i]).xuhao + " " + ((student)al[i]).xuehao + " " + ((student)al[i]).name + " " + ((student)al[i]).score); } Console.ReadLine(); } } }