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

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

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

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

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

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

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

2.设计提示:

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

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

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

答:首先打开 Visual Studio2010,创建一个新的 Windows 窗体应用程序项目。在设计器中,将6个 TextBox 控件和3个 Button 控件添加到窗体上。TextBox 控件分别用于输入学号、姓名、语文、数学、英语、课程名,Button 控件用于触发添加、完成和查询操作。将这6个 TextBox 控件分别命名为 textBox1....textBox6,Button 控件分别命名为button1...button3。双击 Button 控件,在代码中生成按钮的 Click 事件处理程序。保存并运行程序。

具体代码如下:

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 Form1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("欢迎登陆查分系统!");
            lblShow.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++;
                lblShow.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)
        {
            lblShow.Text = "";
            if (textBox6.Text.Trim() != "")
            {
                //计全班学生的平均成绩
                lblShow.Text += "\n\n全班同学的平均成绩为:   " + Convert.ToString(gr.getAverage());
                //统计单科成绩的最高分
                lblShow.Text += "\n\n语文成绩最高分为:" + Convert.ToString(gr.getChineseMax());
                lblShow.Text += "\n\n数学成绩最高分为:" + Convert.ToString(gr.getMathMax());
                lblShow.Text += "\n\n英语成绩最高分为:" + Convert.ToString(gr.getEnglishMax());
                //统计全班前3名的名单
                lblShow.Text += "\n\n全班前三名的名单为:" + gr.getNames();
                //指定课程不及格的学生名单
                lblShow.Text += "\n\n指定科目“ " + textBox6.Text + " ”   不及格的名单:" + gr.getStudentMenu(textBox6.Text);
                //统计指定课程在不同分数段的学生人数百分比
                lblShow.Text += "\n\n指定科目“ " + textBox6.Text + " ”   不同分数段的百分比如下:" + gr.getStudentBFB(textBox6.Text);
                textBox6.Text = "";
            }
            else
            {
                MessageBox.Show("请输入您要查询的课程名称:");
            }

        }
        //可以根据姓名查询指定学生的总成绩
        private void button3_Click(object sender, EventArgs e)
        {
            lblShow.Text = "";
            if (textBox2.Text.Trim() != "")
            {
                double result = gr.getSum(textBox2.Text);
                if (result == -1)
                {
                    MessageBox.Show("该学生不存在!!!");
                }
                else
                {
                    lblShow.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 "不存在(您输入的课程名称不正确)";
            }
        }
    }
}

运行效果图如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,我能帮您快速设计一个简单学生信息管理系统。首先,我们可以定义一个学生类,包含学号姓名、性别、班级和电话等五个属性。然后,我们创建一个列表来存储所有学生对象,实现增、删除、修改和查询等功能。 以下是示例代码: ``` class Student: def __init__(self, id, name, gender, cls, tel): self.id = id self.name = name self.gender = gender self.cls = cls self.tel = tel students = [] def add_student(): id = input("请输入学号:") name = input("请输入姓名:") gender = input("请输入性别:") cls = input("请输入班级:") tel = input("请输入电话:") student = Student(id, name, gender, cls, tel) students.append(student) def del_student(): id = input("请输入要删除的学号:") for student in students: if student.id == id: students.remove(student) print("删除成功!") break else: print("该学号不存在。") def modify_student(): id = input("请输入要修改的学号:") for student in students: if student.id == id: name = input("请输入新姓名(回车表示不修改):") gender = input("请输入新性别(回车表示不修改):") cls = input("请输入新班级(回车表示不修改):") tel = input("请输入新电话(回车表示不修改):") if name: student.name = name if gender: student.gender = gender if cls: student.cls = cls if tel: student.tel = tel print("修改成功!") break else: print("该学号不存在。") def query_student(): id = input("请输入要查询的学号:") for student in students: if student.id == id: print(student.id, student.name, student.gender, student.cls, student.tel) break else: print("该学号不存在。") def show_students(): for student in students: print(student.id, student.name, student.gender, student.cls, student.tel) while True: print(""" **************** 1. 添学生信息 2. 删除学生信息 3. 修改学生信息 4. 查询学生信息 5. 显示所有学生信息 0. 退出系统 **************** """) choice = input("请输入操作编号:") if choice == "1": add_student() elif choice == "2": del_student() elif choice == "3": modify_student() elif choice == "4": query_student() elif choice == "5": show_students() elif choice == "0": break else: print("输入有误,请重新输入!") ``` 以上程序提供了添、删除、修改、查询和显示所有学生信息等五个功能。可以根据实际需要进行扩展和改进。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

空心木偶☜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值