c#排序不够精简,谁有更好的方法?
假设有5个学生,每个学生有6门功课,请设计程序求出各位学生的总分,平均分,求算出谁总分第一,谁总分最后。using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Student
{
string name;
public string Name
{
get { return name; }
set { name = value; }
}
double chinese;
public double Chinese
{
get { return chinese; }
set { chinese = value; }
}
double math;
public double Math
{
get { return math; }
set { math = value; }
}
double english;
public double English
{
get { return english; }
set { english = value; }
}
double politic;
public double Politic
{
get { return politic; }
set { politic = value; }
}
double chemistry;
public double Chemistry
{
get { return chemistry; }
set { chemistry = value; }
}
double biology;
public double Biology
{
get { return biology; }
set { biology = value; }
}
double sum;
public double Sum
{
get { return sum; }
set { sum = value; }
}
double pin;
public double Pin
{
get { return pin; }
set { pin = value; }
}
public Student(string name, double chinese, double math, double english, double politic, double chemistry, double biology)
{
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
this.politic =politic ;
this.chemistry =chemistry ;
this.biology =biology ;
}
}
class Program
{
static void Main(string[] args)
{
Student a = new Student("张一", 58, 52, 58, 96, 54, 56);
Student b = new Student("李二", 50, 52, 58, 96, 54, 56);
Student c = new Student("周三", 58, 52, 68, 96, 54, 56);
Student d = new Student("马四", 58, 52, 58, 96, 54, 96);
Student e = new Student("王五", 58, 32, 58, 96, 54, 56);
double sum = 0;
a.Sum = a.Chinese + a.English + a.Biology + a.Chemistry + a.Math + a.Politic;
a.Pin = a.Sum / 6;
b.Sum = b.Chinese + b.English + b.Biology + b.Chemistry + b.Math + b.Politic;
b.Pin = b.Sum / 6;
c.Sum = c.Chinese + c.English + c.Biology + c.Chemistry + c.Math + c.Politic;
c.Pin = c.Sum / 6;
d.Sum = d.Chinese + d.English + d.Biology + d.Chemistry + d.Math + d.Politic;
d.Pin = d.Sum / 6;
e.Sum = e.Chinese + e.English + e.Biology + e.Chemistry + e.Math + e.Politic;
e.Pin = e.Sum / 6;
double[] mx = new double[] { a.Sum, b.Sum , c.Sum , d.Sum , e.Sum };
Array.Sort(mx);
double[] mn = new double[] { a.Pin, b.Pin, c.Pin, d.Pin, e.Pin};
Array.Sort(mn);
Console.WriteLine("最高分{0},最低分{1},平均分最低是{2},平均分最高是{3}",mx[4],mx[0],mn[0],mn[4]);
Console.ReadLine();
}
}
}
想了半天,想用构造函数解决这个问题,最后还是写了这么多,谁有更好的方法?