老师要求:能查询每个学生的总成绩、显示前三名成绩、单科成绩最高分和不及格的学生名单、全班平均成绩、各科成绩在不同分数段的学生人数百分比。
第一次用C#,写的比较糙,但总的来说还是学到不少的:
1.学会了Windows窗体的建立,基本组件的添加,组件属性的获取(如果在当前窗体下就是this.组件名字.组件属性:this.Grades.Text)、点击按钮事件的编写;
2.C#基本语句,面向对象的编程的思想;
3.老师要求不用数据库,直接把成绩写到类里,这个地方纠结了好久,我的实现方法是新建一个Student类,一个Class类,班级类里包含一个student类的数组,和其他实现上述结果的方法。
界面1:第一个窗体应该是主窗体,我发现主窗体里的类成员、方法,在后面新建的窗体里是可以直接访问的,所以我把学生类和班级类创建在这个CS文件里。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Text1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
}
private void button6_Click(object sender, EventArgs e)
{
Form2 myForm = new Form2();//弹出新窗体
myForm.ShowDialog();
}
}
}
public class FormStudent//创建学生类
{
public string studentsName;//学生姓名
public int Number;//学号
public int Chinese;//语文成绩
public int English;//英语成绩
public int Math;//数学成绩
public int SCORE;//总分
}
public class Class//班级类
{
public FormStudent[] students { get; set; }//一个名字为students的由FormStudent类型组成的数组
public float EnglishYouxiu = 0;//这里是一系列记录分数分布的变量,初始值为0
public float EnglishJige = 0;
public float EnglishBujige = 0;
public int EnopassNumber = 999;//这个用来经过排序后记录第一个没有通过的学生序号
public float MathYouxiu = 0;
public float MathJige = 0;
public float MathBujige = 0;
public int MnopassNumber = 999;
public float ChineseYouxiu = 0;
public float ChineseJige = 0;
public float ChineseBujige = 0;
public int CnopassNumber = 999;
public void three(FormStudent[] studentscore)//冒泡排序获得前三名成绩
{
int length = 3;
if(studentscore.Length>2)
{
length = studentscore.Length;
}
for (int i = 0; i < 3; i++)
{
for (int j = (length - 3); j >= 0; j--)
{
if (studentscore[j].SCORE < studentscore[j + 1].SCORE)
{
FormStudent st = studentscore[j];
studentscore[j] = studentscore[j + 1];
studentscore[j + 1] = st;
}
}
}
}
public float Average(FormStudent[] studentscore, int m)//求均分
{
int Fullscore = 0;
for (int q = 0; q < m; q++)
{
Fullscore = Fullscore + studentscore[q].SCORE;
}
float ave = Fullscore / m;
return ave;
}
public int EnglishBest(FormStudent[] studentscore)//英语最高成绩
{
int k = 0;
int score = studentscore[0].English;
for (int i = 0; i < studentscore.Length - 1; i++)
{
if (studentscore[k].English < studentscore[i].English)
{
k = i;
score = studentscore[i].English;
}
}
return score;
}
public int ChineseBest(FormStudent[] studentscore)//语文最高成绩
{
int k = 0;
int score = studentscore[0].Chinese;
for (int i = 0; i < studentscore.Length - 1; i++)
{
if (studentscore[k].Chinese < studentscore[i].Chinese)
{
k = i;
score = studentscore[i].Chinese;
}
}
return score;
}
public int MathBest(FormStudent[] studentscore)//数学最高成绩
{
int k = 0;
int score = studentscore[0].Math;
for (int i = 0; i < studentscore.Length - 1; i++)
{
if (studentscore[k].Math < studentscore[i].Math)
{
k = i;
score = studentscore[i].Math;
}
}
return score;
}
public FormStudent[] EnglishGrades(FormStudent[] score)//英语成绩分布
{
for (int i = 0; i < score.Length; i++)
{
for (int j = (score.Length - 3); j >= 0; j--)
{
if (score[j].English < score[j + 1].English)
{
FormStudent st = score[j];
score[j] = score[j + 1];
score[j + 1] = st;
}
}
}
float Youxiu = 0;
float Jige = 0;
float Bujige = 0;
bool x = true;//设置M的标志符,意义在于判断是不是已经记录过第一个不及格的人序号,只用记录第一个序号就可以。
int m = score.Length;//记录不及格第一个人的角标,设初始值为数组长度,如果全部及格,比如4个人长度为4 A[4]一定不存在。
for (int i = 0; i < (score.Length - 1) && score[i].Number != 0; i++)
{
if (score[i].English >= 90)
Youxiu++;
else if (score[i].English