3.创建学生类Student,基本构成如下(应适当扩充):
①私有字段name(学生姓名),公有属性Name;
②私有字段score(成绩),公有属性Score;
要求在完善学生类构成的基础上,将所有学生对象存放在一个Student对象数组中,通过方法Sort实现按照Score(成绩)进行降序排序,并输出排序结果。
using System;
namespace the_second_test
{
class Student
{
private string name;
private int score;
private string number;
private string sex;
public string Number
{
get { return number; }
set { number = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public int Score
{
get { return score; }
set
{
if (value > 100 || value < 0)
Console.WriteLine("输入不正确");
else
score = value;
}
}
public string Sex
{
get { return sex; }
set
{
if (value == "男" || value == "女")
sex = value;
else
Console.WriteLine("您的输入有问题");
}
}
public Student(string number, string name, int score, string sex)
{
this.Number = number;
this.Name = name;
this.Score = score;
this.Sex = sex;
}
}
}
class Program
{
static void Decsort(Student[] listStudent)
{
for (int i=0;i<listStudent .Length -1;i++)
{
for (int j=0;j<listStudent .Length -1-i;j++)
{
if (listStudent[j].Score < listStudent[j + 1].Score)
{
Student orderScore;
orderScore = listStudent[j];
listStudent[j] = listStudent[j + 1];
listStudent[j + 1] = orderScore;
}
}
}
}
static void Main(string[] args)
{
Console.Write("请输入要创建Student对象数组的大小");
int n = Convert.ToInt32(Console .ReadLine ());
Student[] studentArray = new Student[n];
for (int i=0;i<n;i++)
{
Console.Write("请输入第{0}个学生编号",i + 1);
string number = Console.ReadLine();
Console.Write("请输入第{0}个学生姓名", i + 1);
string name = Console.ReadLine();
Console.Write("请输入第{0}个学生期末成绩", i + 1);
string score = Console.ReadLine();
Console.Write("请输入第{0}个学生性别", i + 1);
string sex = Console.ReadLine();
studentArray[i] = new Student(number, name, score, sex);
Console.WriteLine();
Console.WriteLine("排序前");
Console.WriteLine("学号\t姓名\t成绩\t性别");
foreach (Student stu in studentArray )
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}",stu .number,stu .Name ,stu .Score,stu .Sex);
}
Decsort(studentArray);
Console.WriteLine("按照成绩降序排序后的结果为:");
Console.WriteLine("学号\t姓名\t成绩\t性别");
Console.WriteLine("学号\t姓名\t成绩\t性别");
foreach (Student stu in studentArray)
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}", stu.number, stu.Name, stu.Score, stu.Sex);
}
}
}
}
这个错误如何修改