//学生成绩信息结构体
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++)
{
Console.WriteLine("请输入第{0}个学生姓名", i + 1);
string StuName = Console.ReadLine();
students[i] = new student();//初始化数组元素
students[i].name = StuName;//对数组元素进行赋值
Console.WriteLine("请输入第{0}个学生成绩(0-100之间)", i + 1);
double score = Convert.ToDouble(Console.ReadLine());
students[i].score = score;//对数组元素进行赋值
//每输入一次成绩相加一次
SumScore += score;
}
//选择排序法对成绩进行排序
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();
}
}
|