这是一个控制台的程序,有一个班级,需要为每一个学生输入各科(语文,数学和英语)考试成绩,包括学生姓名,科目和成绩。1、请依次输入每个学生的各科成绩然后输出。2、请计算各科成绩最好的学生以及分数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;namespace stuConsole {    //定义学生类
    class student {
        //字段定义
        private string name;            //姓名
        private double engScore;        //英语成绩
        private double chScore;          //语文成绩
        private double mathScore;       //数学成绩
        public static bool isEnd = false;
        public static int count;   //记录学生的个数
        public student() {
            count++;
        }        //定义属性,通过属性访问、修改相应字段
        public string Name {
            set { name = value; }
            get { return name; }
        }
        public double EngScore {
            set { engScore = value; }
            get { return engScore; }
        }
        public double CScore {
            set { chScore = value; }
            get { return chScore; }
        }
        public double MathScore {
            set { mathScore = value; }
            get { return mathScore; }
        }
        ArrayList arrayList = new ArrayList(4);        //输入相应的数据
        public void inputText() {
            Console.WriteLine("请输入姓名:");
            Name = Console.ReadLine();
            if (name == "end") {
                student.isEnd = true;
                return;
            }
            Console.WriteLine("请输入英语成绩:");
            EngScore = double.Parse(Console.ReadLine());
            Console.WriteLine("请输入语文成绩:");
            CScore = double.Parse(Console.ReadLine());
            Console.WriteLine("请输入数学成绩:");
            MathScore = double.Parse(Console.ReadLine());
        }
        //输出相应的数据
        public void outputText() {
            Console.WriteLine("当输入姓名为end时结束");
            Console.WriteLine("姓名为:{0}", Name);
            Console.WriteLine("语文成绩为{0}", CScore);
            Console.WriteLine("英语成绩为{0}", EngScore);
            Console.WriteLine("数学成绩为{0}", MathScore);
        }
    }
    class Program {
        static void Main(string[] args) {
            student[] t = new student[100];
            for (int i = 0; i < 100; i++) { t[i] = new student(); }
            student.count = 0;
            while (true) {
                try {                    //student t;
                    t[student.count] = new student();
                    t[student.count].inputText();
                    if (student.isEnd) {
                        break;
                    }
                    t[student.count].outputText();                    Console.ReadLine();
                    student.count++;
                }
                catch (Exception ex) {
                    Console.WriteLine(ex.Message.ToString());
                }
            }
            double empty = 0;
            double empty2 = 0;
            double empty3 = 0;
            int bestMath = 0;
            int bestEng = 0;
            int bestChe = 0;
            for (int j = 0; j < student.count; j++) {
                if (t[j].MathScore > empty) {
                    bestMath = j;
                    empty = t[j].MathScore;
                }
                if (t[j].CScore > empty2) {
                    bestChe = j;
                    empty2 = t[j].CScore;
                }
                if (t[j].EngScore > empty3) {
                    bestEng = j;
                    empty3 = t[j].EngScore;
                }
            }
            Console.WriteLine("数学最好的是:");
            Console.WriteLine(t[bestMath].Name + ",成绩是:" + t[bestMath].MathScore.ToString());
            Console.WriteLine("语文最好的是:");
            Console.WriteLine(t[bestChe].Name + ",成绩是:" + t[bestChe].CScore.ToString());
            Console.WriteLine("英语最好的是:");
            Console.WriteLine(t[bestEng].Name + ",成绩是:" + t[bestEng].EngScore.ToString());
            Console.ReadLine();
        }
    }}该如何改进,是代码更加优秀C#学生管理.Net

解决方案 »

  1.   

    学生类不需要写输入数据的方法在主程序里,for 循环来输入学生实体的属性即可
      

  2.   

    用地道的C#风格去写,而不是带着Java的“口音”。
      

  3.   

    public string Name { get; set; }
      

  4.   

    public static int count;   //记录学生的个数 你是学生? 你有这个属性? 
    还有学生类都有些什么行为? inputText(),outputText()?  不过这挺形象的
    现在的学生上学确实都是整天在inputText(),outputText()
      

  5.   

    public student() {
                count++;
            }学生人数的增加写在构造函数中,个人觉得不妥。。
      

  6.   

    不是C#代码,怎么分开这么像Java
      

  7.   


    1:类、属性、方法的注释用XML的注释比较好
    2:属性如果没什么特殊的处理,直接用Get;Set即可,代码简洁(你都引用了linq了,简写方式是支持的)
    3:建议私有private变量用_开头的小驼峰写法,受保护变量直接使用小驼峰写法,而且不要写public变量,如果是public的可以写成属性方式,方法名称命名建议使用打驼峰写法
    4:ArrayList arrayList = new ArrayList(4);  这变量定义好像没用到 以后建议少定义ArrayList 可以用List<T>来代替,减少拆箱装箱的开销
    5:感觉你没必要写inputText这个变量赋值功能的方法,因为你设置的变量都是属性方式,而且又是对外可写的
    6:IsEnd和count这两个静态变量感觉没必要定义,对学生类来说有用吗?
    7:学生类的功能方法里面建议不要含Console.WriteLine这种控制台关键词,这样的话Student类你放到winform或者webform怎么用?
    8:像类似outputText输出的功能可以重写ToString()来返回你需要输出的字符串
    以上纯属个人看法
      

  8.   

    楼主去看《重构,改善既有的代码>>,
      

  9.   

    1、學生類裏面只留屬性(name,score(對應學科))和初始化的方法(get,set)------對象
    2、如果是要計算班級人數添加新的對象類---class
    3、相應的循環放到主程序裏面,對應的賦值和查找什麽的分別放在小的函數裏面,主程序只負責調用就好
      

  10.   


    public class Student
    {
        public Student(String name)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("姓名name");
            }
            this._name = name;
        }
        private string _name;
        private double _engScore;
        private double _chScore;
        private double _mathScore;    /// <summary>
        /// 姓名
        /// </summary>
        public string Name
        {
            private set { this._name = value; }
            get { return this._name; }
        }
        /// <summary>
        /// 英语成绩
        /// </summary>
        public double EngScore
        {
            set
            {
                if (value < 0 || value > 100)
                {
                    throw new InvalidOperationException("英语成绩[0-100]");
                }
                this._engScore = value;
            }
            get { return this._engScore; }
        }
        /// <summary>
        /// 语文成绩
        /// </summary>
        public double CScore
        {
            set
            {
                //value检测
                this._chScore = value;
            }
            get { return this._chScore; }
        }
        /// <summary>
        /// 数学成绩
        /// </summary>
        public double MathScore
        {
            set
            {
                //value检测
                this._mathScore = value;
            }
            get { return this._mathScore; }
        }    public override string ToString()
        {
            return String.Format("[{0}:语文 {1},英语 {2},数学 {3}]\r\n",
                this.Name,
                this.CScore.ToString(),
                this.EngScore.ToString(),
                this.MathScore.ToString());
        }
    }
    class Program
    {
        public static Student GetInputStudent()
        {
            Console.WriteLine("请输入姓名:");
            String name = Console.ReadLine();
            //异常判断
            Student student = new Student(name);
            Console.WriteLine("请输入英语成绩:");
            //异常判断
            student.EngScore = double.Parse(Console.ReadLine());
            Console.WriteLine("请输入语文成绩:");
            student.CScore = double.Parse(Console.ReadLine());
            Console.WriteLine("请输入数学成绩:");
            student.MathScore = double.Parse(Console.ReadLine());
            return student;
        }
        static void Main(string[] args)
        {
            Student[] students = new Student[100];
            for (int i = 0; i < 100; i++)
            {
                try
                {
                    students[i] = GetInputStudent();
                    Console.WriteLine(students[i]);
                    Console.WriteLine("当输入end时结束");
                    if (String.Equals(Console.ReadLine(), "end", StringComparison.OrdinalIgnoreCase))
                    {
                        break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
    }
    先改一点吧
      

  11.   


    版主不说我都么注意,LZ的风格还真是Java的。次奥。
      

  12.   

    先从语言规范的方面来说说吧,C#使用PascalCase,而不是Java的camelCase,所有的类名、方法名都应该以大写字母开头。
    还有就是类的设计,你的这个类中又了很多不必要的字段、方法,应该删除。
    另外一个就是字符串相等的逻辑判断,你的代码是if (name == "end"){/*code...*/},但是推荐使用String.Equals(String,StringComparison),StringComparison.OrdinalIgnoreCase可以忽略大小写。