C#复习,输入学生信息排列成绩

时间:2023-03-08 21:05:39

C#复习:
在控制台程序中使用结构体、集合,完成下列要求
项目要求:
一、连续输入5个学生的信息,每个学生都有以下4个内容:
1、序号 - 根据输入的顺序自动生成,不需要手动填写,如输入第一个学生的序号是1,第二个是2,以此类推
2、学号 - 必填,如:S001,S002... 以此类推
3、姓名 - 必填
4、成绩 - 大于等于0,小于等于100

以上内容必须按照要求填写,请写好相应的验证,如果没填写正确,则让用户重复填写到正确为止

二、5个学生信息都输入完毕后,按照分数从高到低的顺序将学生信息展示出来
显示格式如下:

==============学生成绩展示=================
序号 学号 姓名 成绩
3 S003 张三 100
1 S001 李四 99
2 S002 王五 98
...
...

-------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections; namespace ConsoleApplication4
{
class Program
{
struct swe
{
public int xuhao;
public string xuehao;
public string xingming;
public double chengji;
}
static void Main(string[] args)
{
ArrayList al = new ArrayList();
Console.WriteLine("请输入班级人数:");
int n = int.Parse(Console.ReadLine());
for (int i = ; i <= n; i++)
{
swe s = new swe();
s.xuhao = i;
while (true)
{
Console.WriteLine("请输入第{0}个学生的学号:", i);
s.xuehao = Console.ReadLine();
if (s.xuehao == "")
{
Console.WriteLine("学号不能为空!");
}
else
{
break;
}
}
while (true)
{
Console.WriteLine("请输入第{0}个学生的姓名:", i);
s.xingming = Console.ReadLine();
if (s.xingming == "")
{
Console.WriteLine("姓名不能为空!");
}
else
{
break;
}
}
while (true)
{
Console.WriteLine("请输入第{0}个学生的成绩;", i);
try
{
s.chengji = int.Parse(Console.ReadLine());
if (s.chengji >= && s.chengji <= )
{
break;
}
else
{
Console.WriteLine("成绩必须在0~100之间!");
}
}
catch
{
Console.WriteLine("成绩输入有误!");
}
}
al.Add(s);
Console.WriteLine();
for (int e = ; e < al.Count - ; e++)
{
for (int j = e + ; j < al.Count; j++)
{
swe s1 = (swe)al[e];
swe s2 = (swe)al[j];
if (s1.chengji < s2.chengji)
{
swe zhong = (swe)al[e];
al[e] = al[j];
al[j] = zhong;
}
}
}
}
Console.WriteLine("序号 学号 姓名 成绩 ");
foreach (object qq in al)
{
Console.WriteLine(((swe)qq).xuhao + " " +
((swe)qq).xuehao + " " +
((swe)qq).xingming + " " +
((swe)qq).chengji);
}