| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1072 人关注过本帖
标题:c#排序不够精简,谁有更好的方法?
取消只看楼主 加入收藏
qq645485165
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:76
专家分:197
注 册:2013-3-16
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:3 
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();
        }   
    }
}

想了半天,想用构造函数解决这个问题,最后还是写了这么多,谁有更好的方法?
搜索更多相关主题的帖子: namespace 设计程序 Chinese public 
2013-03-19 22:07
qq645485165
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:76
专家分:197
注 册:2013-3-16
收藏
得分:0 
回复 2楼 yhlvht
调到Student方法里,和在Main差不多,可是得出的最高分,最低分不知道是谁的啊?你有什么好方法没?
2013-03-20 11:01
qq645485165
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:76
专家分:197
注 册:2013-3-16
收藏
得分:0 
回复 5楼 yhlvht
这个我知道,但是还是不知道谁是最高分啊
2013-03-20 16:10
qq645485165
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:76
专家分:197
注 册:2013-3-16
收藏
得分:0 
谢谢啊!
2013-03-21 12:02
快速回复:c#排序不够精简,谁有更好的方法?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017410 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved