(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 "不存在(您输入的课程名称不正确)";
}
}
}
}
运行效果图如下: