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

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

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

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

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

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

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

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

设计提示:

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

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

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

 

设计界面

编写代码

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace c

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            label7.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++;

                label7.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)

        {

            label7.Text = "";

            if (textBox6.Text.Trim() != "")

            {

                //(2)可以统计全班学生的平均成绩

                label7.Text += "\n\n全班同学的平均成绩为:   " + Convert.ToString(gr.getAverage());

 

                //(3)可以统计单科成绩的最高分

                label7.Text += "\n\n语文成绩最高分为:" + Convert.ToString(gr.getChineseMax());

                label7.Text += "\n\n数学成绩最高分为:" + Convert.ToString(gr.getMathMax());

                label7.Text += "\n\n英语成绩最高分为:" + Convert.ToString(gr.getEnglishMax());

 

                //(4)可以统计全班前3名的名单

                label7.Text += "\n\n全班前三名的名单为:" + gr.getNames();

 

                //(5)可以指定课程不及格的学生名单

                label7.Text += "\n\n指定科目“ " + textBox6.Text + " ”   不及格的名单:" + gr.getStudentMenu(textBox6.Text);

 

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

                label7.Text += "\n\n指定科目“ " + textBox6.Text + " ”   不同分数段的百分比如下:" + gr.getStudentBFB(textBox6.Text);    

textBox6.Text = "";        

            }

            else

            {

                MessageBox.Show("请输入您要查询的课程名称:");

            }

 

        }

 

        //(1)可以根据姓名查询指定学生的总成绩

        private void button3_Click(object sender, EventArgs e)

        {

            label7 .Text ="";

            if (textBox2.Text.Trim() != "")

            {

                double result = gr.getSum(textBox2.Text);

                if (result == -1)

                {

                    MessageBox.Show("该学生不存在!!!");

                }

                else

                {

                    label7.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

        }

        //查询指定学生的总成绩(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;

        }

 

        //可以指定课程不及格的学生名单(5)

        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 "不存在(您输入的课程名称不正确)";

            }

        }

 

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

        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 "不存在(您输入的课程名称不正确)";

            }

        }

 

    }

}

 

运行结果

  • 30
    点赞
  • 126
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值