用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/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM