用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刪除。



猜您在找 【C語言】利用文本編輯軟件在磁盤上建立一個有n個學生的學號、姓名及英語、數學和計算機三門課程成績的數據文件。編寫程序將數據文件讀入,且求出每個學生的平均成績。 用C# 設計一個 Windows應用程序,在該程序中首先構造一個學生基本類,再分別構造小學生、中學生、大學生等派生類,當輸入相關數據,單擊不同的按鈕(小學生、中學生、大學生)將分別創建不同的學生對象,並輸出當前學生總人數,該學生的姓名、學生類型和平均成績。 有五個學生,每個學生有3門課(語文、數學、英語)的成績, * 寫一個程序接收從鍵盤輸入學生的信息,輸入格式為:name,30,30,30(姓名,三門課成績) 使用Python批量獲取學生期末考試成績 用C#設計控制台應用程序,輸入若干學生的英語和數學成績,求出總分,並按總分從高到低排序。要求設計一個學生類 Student,所有學生對象存放在一個 Student對象數組中,通過一個方法對其按照總分進行降序排序,最后輸出排序后的結果 C++習題 對象轉換(定義一個Teacher(教師)類(教師號,姓名,性別,薪金)和一個Student(學生)類(學號,姓名,性別,成績)編寫程序,將一個Student對象(學生)轉換為Teacher(教師)類。 設計一個班級類和一個學生類:1.學生類屬性:姓名,學號,成績(整數即可),定義相關構造方法;定義返回學生信息字符串的方法;2.班級類屬性:班級名,人數,成員;其中成員是一個學生類的數組; 【JAVA】【作業向】第一題:本學期一班級有n名學生,m門課程。現要求對每門課程的成績進行統計:平均成績、最高成績、最低成績,並統計考試成績的分布律。 JAVA編程:有五個學生,每個學生有3門課(語文、數學、英語)的成績, 編寫Java應用程序。首先,定義描述學生的類——Student,包括學號(int)、 姓名(String)、年齡(int)等屬性;二個方法:Student(int stuNo,String name,int age) 用於對對象的初始化,outPut()用於輸出學生信息。其次,再定義一個主類—— TestClass,在主類的main方法中創建多個Student類的對象,使用這些對象來測 試Stud
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM