按成绩进行排序,按成绩由高到低依次输出学生的姓名和成绩作者: 陈育梅 时间: 2012-10-26 11:44
//学生成绩信息结构体
struct student
{
public double score;
public string name;
}
class Program
{
public static void Main(string[] args)
{
try
{
//学生总成绩
double SumScore = 0.0;
//学生平均成绩
double average = 0.0;
//提示用户输入班级人数
Console.WriteLine("请输入班级人数:");
int number = Convert.ToInt32(Console.ReadLine());
//初始化结构
student[] students = new student[number];
//循环输入学生的姓名及成绩
for (int i = 0; i < students.Length; i++)
{
//选择排序法对成绩进行排序
for (int i = 0; i < students.Length; i++)
{
int max = i;
for (int j = i + 1; j < students.Length; j++)
{
if (students[max].score < students[j].score)
{
max = j;
}
}
if (max != i)
{
student s = students[max];
students[max] = students[i];
students[i] = s;
}
}
//计算机平均成绩
average = (double)SumScore / number;
Console.WriteLine("该班学生平均成绩为:{0}", average);
Console.WriteLine("该班的成绩排名为:");
//遍历数组中的每一个元素s
foreach (student s in students)
{
Console.WriteLine("姓名={0} 成绩={1}", s.name, s.score);
}
}
//捕获异常
catch
{
Console.WriteLine("你的输入有误,请重新运行本程序");
}
Console.ReadKey();