用C#设计一个 Windows应用程序,在该程序中定义一个学生类和班级类,以处理每个学生的学号、姓名、语文、数学和英语3门课程的期末考试成绩

1.题目要求:

用C#设计一个 Windows应用程序,在该程序中定义一个学生类和班级类,以处理每个学生的学号、姓名、语文、数学和英语3门课程的期末考试成绩,要求:

(1)能根据姓名查询指定学生的总成绩。

(2)能统计全班学生的平均成绩。

(3)能统计单科成绩最高分。

(4)能统计全班前3名的名单

(5)能统计指定课程不及格的学生名单。

  (6)能统计指定课程在不同分数段的学生人数百分比。

2.设计提示:

(1)定义一个 Student学生类,包含字段(学号、姓名、语文成绩、数学成绩、英语成绩)和属性(总成绩)等

(2)定义一个 Grade班级类,包含一个 Student类型的数组(用来保存全班学生的信息)以及若干个实现上述要求的方法等。

(3)设计用户操作界面,首先让用户能输入一个学生的信息,当单击"添加”按钮时把这些信息添加到班级对象的学生数组中。单击完成”按钮调用班级类的方法来显示各种统计结果。当用户输入了学生姓名并且单击"查询"按钮时显示该学生的总成绩。

3.来吧展示,代码如下:

using System;
using System.Windows.Forms;

namespace Final_Exam_3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("欢迎登陆查分系统!");
            richTextBox1.Text = "";
        }
        Student[] st1 = new Student[100];
        Grade gr = new Grade();
        int i = 0;
        private void button1_Click(object sender, EventArgs e) 
        {
            try
            {
                st1[i] = new Student(textBox1.Text, textBox2.Text, Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text), Convert.ToInt32(textBox5.Text));
                gr.Add(st1[i]);
                i++;
                richTextBox1.Text = "成功添加" + i + "个学生的信息";
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";
                textBox6.Text = "";
            }
            catch
            {
                MessageBox.Show("输入完善的学生信息进行添加!!!");
            }
        }
        //点击完成按钮显示各种查询结果
        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
            if (textBox6.Text.Trim() != "")
            {
                //计全班学生的平均成绩
                richTextBox1.Text += "\n\n全班同学的平均成绩为:   " + Convert.ToString(gr.getAverage());
                //统计单科成绩的最高分
                richTextBox1.Text += "\n\n语文成绩最高分为:" + Convert.ToString(gr.getChineseMax());
                richTextBox1.Text += "\n\n数学成绩最高分为:" + Convert.ToString(gr.getMathMax());
                richTextBox1.Text += "\n\n英语成绩最高分为:" + Convert.ToString(gr.getEnglishMax());
                //统计全班前3名的名单
                richTextBox1.Text += "\n\n全班前三名的名单为:" + gr.getNames();
                //指定课程不及格的学生名单
                richTextBox1.Text += "\n\n指定科目“ " + textBox6.Text + " ”   不及格的名单:" + gr.getStudentMenu(textBox6.Text);
                //统计指定课程在不同分数段的学生人数百分比
                richTextBox1.Text += "\n\n指定科目“ " + textBox6.Text + " ”   不同分数段的百分比如下:" + gr.getStudentBFB(textBox6.Text);
                textBox6.Text = "";
            }
            else
            {
                MessageBox.Show("请输入您要查询的课程名称:");
            }
        }
        //可以根据姓名查询指定学生的总成绩
        private void button3_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
            if (textBox2.Text.Trim() != "")
            {
                double result = gr.getSum(textBox2.Text);
                if (result == -1)
                {
                    MessageBox.Show("该学生不存在!!!");
                }
                else
                {
                    richTextBox1.Text = "学生:" + textBox2.Text + "   的总成绩为:" + Convert.ToString(result);
                    textBox2.Text = "";
                }
            }
            else
            {
                MessageBox.Show("请输入您要查询的学生姓名");
            }
        }
    }
    public class Student
    {
        public string sno;
        public string name;
        public double chinese;
        public double math;
        public double english;
        public Student(string sno, string name, double chinese, double math, double english)
        {
            this.sno = sno;
            this.name = name;
            this.chinese = chinese;
            this.math = math;
            this.english = english;
        }
        public double Sum
        {
            get { return chinese + math + english; }           
        }
    }
    public class Grade
    {
        Student[] stu = new Student[100];  //存放班级中的每个同学的信息
        double[] sum2 = new double[100];  //存放每个同学的总成绩
        int i = 0; //学生人数
        public Grade() { }
        public void Add(Student s)
        {
            stu[i] = s;  //添加每个学生到班级中
            sum2[i] = stu[i].Sum;  //保存每个学生的总成绩
            i++;  //学生人数加1
        }
        //查询指定学生的总成绩
        int x = 0;
        int k = 0;
        bool flag = false;
        public double getSum(string s)
        {
            for (x = 0; x < i; x++)
            {
                if (stu[x].name == s)
                {
                    k = x;
                    flag = true;
                }
            }
            if (flag == true)
            {
                return sum2[k];  //如果该学生在班级中,则返回该学生的总成绩,否则,返回-1
            }
            else
            {
                return -1;
            }
        }
        //统计全班同学的平均成绩(2)
        double avg = 0;
        public double getAverage()
        {
            for (int aa = 0; aa < i; aa++)
            {
                avg += sum2[aa];
            }
            return avg / i;
        }
        //统计语文成绩最高分(3—1)
        double maxChinese = 0;
        public double getChineseMax()
        {
            for (int z = 0; z < i; z++)
            {
                if (stu[z].chinese > maxChinese)
                {
                    maxChinese = stu[z].chinese;
                }
            }
            return maxChinese;
        }
        //统计数学成绩最高分(3—2)
        double maxMath = 0;
        public double getMathMax()
        {
            for (int z = 0; z < i; z++)
            {
                if (stu[z].math > maxMath)
                {
                    maxMath = stu[z].math;
                }
            }
            return maxMath;
        }
        //统计英语成绩最高分(3—3)
        double maxEnglish = 0;
        public double getEnglishMax()
        {
            for (int z = 0; z < i; z++)
            {
                if (stu[z].english > maxEnglish)
                {
                    maxEnglish = stu[z].english;
                }
            }
            return maxEnglish;
        }
        //可以统计全班前3名的名单(4)——根据总成绩数组sum2[]将所有学生即stu数组重新排序
        public string getNames()
        {
            Student[] t = new Student[1];  //中间变量(通过Student类型的中间变量,根据每个学生的总成绩重新排列学生类的全部信息)
            t[0] = new Student("", "", 0, 0, 0);
            double t2 = 0;
            for (int xx = 0; xx < i - 1; xx++)
            {
                for (int yy = xx + 1; yy < i; yy++)
                {
                    if (sum2[yy] > sum2[xx])
                    {
                        t2 = sum2[yy];
                        sum2[yy] = sum2[xx];
                        sum2[xx] = t2;
                        t[0] = stu[yy];
                        stu[yy] = stu[xx];
                        stu[xx] = t[0];
                    }
                }
            }
            return " " + stu[0].name + " " + stu[1].name + " " + stu[2].name;
        }
        //可以指定课程不及格的学生名单
        string md = "";
        public string getStudentMenu(string s)
        {
            if (s == "语文")
            {
                for (int x = 0; x < i; x++)
                {
                    if (stu[x].chinese < 60)
                    {
                        md += " " + stu[x].name;
                    }
                }
                return " " + md;
            }
            else if (s == "数学")
            {
                for (int x = 0; x < i; x++)
                {
                    if (stu[x].math < 60)
                    {
                        md += " " + stu[x].name;
                    }
                }
                return " " + md;
            }
            else if (s == "英语")
            {
                for (int x = 0; x < i; x++)
                {
                    if (stu[x].english < 60)
                    {
                        md += " " + stu[x].name;
                    }
                }
                return " " + md;
            }
            else
            {
                return "不存在(您输入的课程名称不正确)";
            }
        }
        //统计指定课程在不同分数段的学生人数百分比       
        public string getStudentBFB(string s)
        {
            if (s == "语文")
            {
                double yw1 = 0; double yw2 = 0; double yw3 = 0; double yw4 = 0; double yw5 = 0;
                for (int z = 0; z < i; z++)
                {
                    if (stu[z].chinese <= 100 && stu[z].chinese >= 90)
                    {
                        yw1++;
                    }
                    else if (stu[z].chinese < 90 && stu[z].chinese >= 80)
                    {
                        yw2++;
                    }
                    else if (stu[z].chinese < 80 && stu[z].chinese >= 70)
                    {
                        yw3++;
                    }
                    else if (stu[z].chinese < 70 && stu[z].chinese >= 60)
                    {
                        yw4++;
                    }
                    else
                    {
                        yw5++;
                    }
                }
                return "\n90-100:" + (yw1 / i) * 100.0 + "%\n80-90:" + (yw2 / i) * 100.0 + "%\n70-80:" + (yw3 / i) * 100.0
                    + "%\n60-70:" + (yw4 / i) * 100.0 + "%\n60以下:" + (yw5 / i) * 100.0 + "%";
            }
            else if (s == "数学")
            {
                double yw1 = 0; double yw2 = 0; double yw3 = 0; double yw4 = 0; double yw5 = 0;
                for (int z = 0; z < i; z++)
                {
                    if (stu[z].chinese <= 100 && stu[z].chinese >= 90)
                    {
                        yw1++;
                    }
                    else if (stu[z].chinese < 90 && stu[z].chinese >= 80)
                    {
                        yw2++;
                    }
                    else if (stu[z].chinese < 80 && stu[z].chinese >= 70)
                    {
                        yw3++;
                    }
                    else if (stu[z].chinese < 70 && stu[z].chinese >= 60)
                    {
                        yw4++;
                    }
                    else
                    {
                        yw5++;
                    }
                }
                return "\n90-100:" + (yw1 / i) * 100.0 + "%\n80-90:" + (yw2 / i) * 100.0 + "%\n70-80:" +
                    (yw3 / i) * 100.0 + "%\n60-70:" + (yw4 / i) * 100.0 + "%\n60以下:" + (yw5 / i) * 100.0 + "%";
            }
            else if (s == "英语")
            {
                double yw1 = 0; double yw2 = 0; double yw3 = 0; double yw4 = 0; double yw5 = 0;
                for (int z = 0; z < i; z++)
                {
                    if (stu[z].chinese <= 100 && stu[z].chinese >= 90)
                    {
                        yw1++;
                    }
                    else if (stu[z].chinese < 90 && stu[z].chinese >= 80)
                    {
                        yw2++;
                    }
                    else if (stu[z].chinese < 80 && stu[z].chinese >= 70)
                    {
                        yw3++;
                    }
                    else if (stu[z].chinese < 70 && stu[z].chinese >= 60)
                    {
                        yw4++;
                    }
                    else
                    {
                        yw5++;
                    }
                }
                return "\n90-100:" + (yw1 / i) * 100.0 + "%\n80-90:" + (yw2 / i) * 100.0 + "%\n70-80:" +
                    (yw3 / i) * 100.0 + "%\n60-70:" + (yw4 / i) * 100.0 + "%\n60以下:" + (yw5 / i) * 100.0 + "%";
            }
            else
            {
                return "不存在(您输入的课程名称不正确)";
            }
        }
    }
}

 

4.运行结果如下:

首先,我们依次输入三个学生的信息和各科成绩

 

点击添加依次录入3条记录

 

 

 课程名中输入想要查询的课程,比如:数学

 

 输入课程名后,点击完成,就可以得到题目想要的信息

 

 同样,也可以查询某个同学的总成绩,只需要输入学生姓名,点击查询即可

 

 

我是小关,关注我,带你从初级入门编程
希望能帮到大家,问你们要一个赞,你们会给吗,谢谢大家
版权声明:本文版权归作者(@攻城狮小关)和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
大家写文都不容易,请尊重劳动成果~
交流加Q:1909561302
CSDN地址https://blog.csdn.net/Mumaren6/

 

posted @ 2021-01-06 15:20  攻城狮小关  阅读(2954)  评论(0编辑  收藏  举报