课程名称 C#程序设计
实验名称 实验一
肆、第四题
一、实验题目
编写一个控制台程序,在该程序中定义一个学生类和班级类,以处理每个学生的学号、姓名、语文、数学和英语3门课程的期末考试成绩。
二、实验要求
(1)能查询每个学生的总成绩;
(2)能显示单科成绩最高分和该科不及格的学生名单;
(3)能统计全班学生的平均成绩;
(4)能对班级学生的各科成绩进行排序
三、实验代码以及执行结果
1、Student类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 第四题
{
class Student
{
private string number; //学号
private string name; //姓名
private double chineseScore; //语文成绩
private double mathScore; //数学成绩
private double englishScore; //英语成绩
private double avgScore; //三科的平均成绩
public Student(string number, string name, double chineseScore, double mathScore, double englishScore)
{
this.number = number;
this.name = name;
this.chineseScore = chineseScore;
this.mathScore = mathScore;
this.englishScore = englishScore;
avgScore = (chineseScore + mathScore + englishScore) / 3;
}
public double ChineseScore { get => chineseScore; set => chineseScore = value; }
public double MathScore { get => mathScore; set => mathScore = value; }
public double EnglishScore { get => englishScore; set => englishScore = value; }
public double AvgScore { get => avgScore; set => avgScore = value; }
public string Name { get => name; set => name = value; }
public void showMessage() //展示当前学生的信息
{
Console.WriteLine("姓名:{0} 学号:{1} 语文成绩:{2} 数学成绩:{3} 英语成绩:{4}\n", name, number, chineseScore, mathScore, englishScore);
}
public double totalScore() //获取当前学生的语数外总成绩
{
return chineseScore + mathScore + englishScore;
}
}
}
2、Class类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 第四题
{
class Class
{
private string name; //班级的名字
private List<Student> students; //学生数组
private int length = 0; //学生数组的实际长度
private double avgScore = 0; //全班学生三科的平均成绩
public string Name { get => name; set => name = value; }
public Class(string name,List<Student> students, int length)
{
this.name = name;
this.students = students;
this.length = length;
}
public void showTotal(string name) //根据输入的学生名字来查询该学生的总成绩
{
foreach(Student student in students)
{
if(student.Name == name)
{
Console.WriteLine("{0}的总成绩为:{1}\n", name, student.totalScore());
return;
}
}
Console.WriteLine("{0}内没有{1}同学!\n",this.name,name);
return;
}
public void showMessage(string subject) //根据输入的单科课程名称显示该科成绩最高分和不及格的学生信息
{
double maxScore = 0; //最高分
int maxNum = 0; //最高分所在学生下标
if(subject == "语文")
{
maxScore = students[0].ChineseScore;
for (int i = 1;i < length; i++)
{
if (students[i].ChineseScore > maxScore)
{
maxScore = students[i].ChineseScore;
maxNum = i;
}
if (students[i].ChineseScore < 60)
{
Console.WriteLine("{0}成绩不及格的学生信息:", subject);
students[i].showMessage();
}
}
}
else if(subject == "数学")
{
maxScore = students[0].MathScore;
for (int i = 1; i < length; i++)
{
if (students[i].MathScore > maxScore)
{
maxScore = students[i].MathScore;
maxNum = i;
}
if (students[i].ChineseScore < 60)
{
Console.WriteLine("{0}成绩不及格的学生信息:", subject);
students[i].showMessage();
}
}
}
else if(subject == "英语")
{
maxScore = students[0].EnglishScore;
for (int i = 1; i < length; i++)
{
if (students[i].EnglishScore > maxScore)
{
maxScore = students[i].EnglishScore;
maxNum = i;
}
if (students[i].ChineseScore < 60)
{
Console.WriteLine("{0}成绩不及格的学生信息:", subject);
students[i].showMessage();
}
}
}
else
{
Console.WriteLine("课程名称输入错误!\n");
return;
}
Console.WriteLine("{0}成绩最高的学生信息:", subject);
students[maxNum].showMessage();
}
public double getAvgScore() //求全班学生的平均成绩
{
for(int i = 0;i < length; i++)
{
avgScore += students[i].AvgScore;
}
return avgScore / length;
}
public void sortScore(string subject) //根据输入的单科课程名称对班级学生进行排序并显示排序后的学生信息
{
if (subject == "语文")
{
students.Sort(new chineseScoreComparer());
}
else if (subject == "数学")
{
students.Sort(new mathScoreComparer());
}
else if (subject == "英语")
{
students.Sort(new englishScoreComparer());
}
else
{
Console.WriteLine("课程名称输入错误!\n");
return;
}
Console.WriteLine("根据{0}成绩排序后的学生名单:", subject);
for (int i = 0;i < length;i++)
{
students[i].showMessage();
}
}
}
// 语文成绩比较器
class chineseScoreComparer : IComparer<Student>
{
//实现升序
public int Compare(Student student1, Student student2)
{
return (student1.ChineseScore.CompareTo(student2.ChineseScore));
}
}
// 数学成绩比较器
class mathScoreComparer : IComparer<Student>
{
//实现升序
public int Compare(Student student1, Student student2)
{
return (student1.MathScore.CompareTo(student2.MathScore));
}
}
// 英语成绩比较器
class englishScoreComparer : IComparer<Student>
{
//实现升序
public int Compare(Student student1, Student student2)
{
return (student1.EnglishScore.CompareTo(student2.EnglishScore));
}
}
}
3、主类:
using System;
using System.Collections.Generic;
namespace 第四题
{
class Program
{
static void Main(string[] args)
{
Student student1 = new Student("20190441417", "张三", 59, 60, 61);
Student student2 = new Student("20190441418", "李四", 85, 95, 85);
Student student3 = new Student("20190441419", "王五", 90, 90, 95);
List<Student> students = new List<Student> {student1,student2,student3 };
Class class1 = new Class("初二(一)班",students, 3);
class1.showTotal(student1.Name);
class1.showTotal(student2.Name);
class1.showTotal(student3.Name);
class1.showTotal("二麻子");
Console.WriteLine("{0}全班学生的平均成绩为:{1}\n", class1.Name, class1.getAvgScore());
class1.showMessage("语文");
class1.showMessage("数学");
class1.showMessage("英语");
class1.showMessage("物理");
class1.sortScore("语文");
class1.sortScore("数学");
class1.sortScore("英语");
}
}
}
4、执行结果:
四、实验总结
通过这次实验,我学会了在使用Console.Writeline() 时
Console.WriteLine("姓名:{0} 学号:{1} 语文成绩:{2} 数学成绩:{3} 英语成绩:{4}\n", name, number, chineseScore, mathScore, englishScore);
使用这种方式输出,看起来更舒服,且编写输出格式也更方便。