C# 定义全班学生成绩类,包括姓名,学号,C++成绩,英语成绩,数学成绩,平均成绩

设计实现下面4个方法:(1)全班成绩的输入(2)求出每一个同学的平均成绩(3)按平均成绩升序排序(4)输出全班成绩
最新回答
是我,辜负了爱』

2024-11-26 10:05:10

兄弟咱写了1个小时,格式规范,且经过严密测试,完成你所需要功能,且还有人性化设计,并且为你提供了可视化测试,我为你考虑的周到吧,不懂就问我
努力吧~~~
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

///控制台测试
static void Main(string[] args)
{
#region Student Text
AllStudent all = new AllStudent(3);
all.AddAllreslut();
all.RtAvg();
all.printStu();
all.sorting();
Console.WriteLine("(冒泡排序)排序后成绩如下:");
all.printStu();

#endregion
}
以下是单个学生类和整体学生类

#region Student类
public class Students
{

#region 构造函数
public Students()
{
}

public Students(string name, string number, float Cres, float Elys, float maths)
{
_name = name;
_number = number;
_Cres = Cres;
_ely = Elys;
_math = maths;
_avg = (Cres + Elys + maths) / 3;
}

#endregion

#region 字段
private string _name;

public string Name
{
get { return _name; }
set { _name = value; }
}

private string _number;

public string Number
{
get { return _number; }
set { _number = value; }
}

private float _Cres;

public float Cres
{
get { return _Cres; }
set { _Cres = value; }
}

private float _ely;

public float Ely
{
get { return _ely; }
set { _ely = value; }
}

private float _math;

public float Math
{
get { return _math; }
set { _math = value; }
}

private float _avg;

public float Avg
{
get { return _avg; }
set { _avg = value; }
}

#endregion
}
#endregion

#region 全体学生类
public class AllStudent
{

#region 构造函数
public AllStudent(int cout)
{
_cout = cout;
_stuList = new List<Students>();
}
#endregion

#region 字段和属性
private int _cout;

public int Cout
{
get { return _cout; }
set { _cout = value; }
}

private List<Students> _stuList;

public List<Students> StuList
{
get { return _stuList; }
set { _stuList = value; }
}
#endregion

#region 学生成绩录入方法
public void AddAllreslut()
{
for (int i = 0; i < _cout; i++)
{
string[] strs = new string[5];
Console.WriteLine("请输入学生姓名:");
strs[0] = Console.ReadLine();
Console.WriteLine("请输入学生学号:");
strs[1] = Console.ReadLine();
Console.WriteLine("请输入学生C++成绩:");
strs[2] = Console.ReadLine();
if (!Isfloat(strs[2]))
{
Console.WriteLine("请输入正确的成绩:");
strs[2] = Console.ReadLine();
}
else
{
if (float.Parse(strs[2]) > 100)
{
Console.WriteLine("请输入正确的成绩:");
strs[2] = Console.ReadLine();
}
}
Console.WriteLine("请输入学生英语成绩:");
strs[3] = Console.ReadLine();
if (!Isfloat(strs[3]))
{
Console.WriteLine("请输入正确的成绩:");
strs[3] = Console.ReadLine();
}
else
{
if (float.Parse(strs[3]) > 100)
{
Console.WriteLine("请输入正确的成绩:");
strs[3] = Console.ReadLine();
}
}
Console.WriteLine("请输入学生数学成绩:");
strs[4] = Console.ReadLine();
if (!Isfloat(strs[4]))
{
Console.WriteLine("请输入正确的成绩:");
strs[4] = Console.ReadLine();
}
else
{
if (float.Parse(strs[4]) > 100)
{
Console.WriteLine("请输入正确的成绩:");
strs[4] = Console.ReadLine();
}
}
Students student = new Students(strs[0], strs[1], float.Parse(strs[2]), float.Parse(strs[3]), float.Parse(strs[4]));
Console.WriteLine(strs[0] + "同学的平均成绩为:" + student.Avg);
Console.WriteLine();
_stuList.Add(student);
}
}
#endregion

#region 按学号查询平均成绩
public void RtAvg()
{
Console.WriteLine("请输入要查询平均成绩学生的学号:");
string number = Console.ReadLine();
float avg = RtAvg(number);
if (avg != 0)
{
Console.WriteLine(number + "的平均成绩为:" + avg);
Console.ReadKey();
}
else
{
Console.WriteLine("没有该学号学生成绩!");
Console.ReadKey();
}
}

public float RtAvg(string number)
{
for (int i = 0; i < _stuList.Count; i++)
{
if (_stuList[i].Number.Trim() == number.Trim())
{
return _stuList[i].Avg;
}
}
return 0;
}
#endregion

#region 按平均分排序方法
public void sorting()
{
List<Students> list = new List<Students>();
for (int i = 0; i < _stuList.Count; i++)
{
Students stus = new Students();
for (int j = 0; j < _stuList.Count - i - 1; j++)
{
if (_stuList[j].Avg > _stuList[j + 1].Avg)
{
stus = _stuList[j];
_stuList[j] = _stuList[j + 1];
_stuList[j + 1] = stus;
}
else
{
stus = _stuList[j + 1];
}
}
if (i == _stuList.Count - 1)
{
stus = _stuList[0];
}
list.Add(stus);
}
_stuList = list;
}
#endregion

#region 输出所有学生成绩
public void printStu()
{
Console.WriteLine("一下是所有学生信息:");
for (int i = 0; i < _stuList.Count; i++)
{
Console.WriteLine(_stuList[i].Name + "同学的基本信息:");
Console.WriteLine("学号:" + _stuList[i].Number + " C++成绩为:" + _stuList[i].Cres + " 英语成绩为:" + _stuList[i].Ely + " 数学成绩为:" + _stuList[i].Math+" 平均成绩为:"+_stuList[i].Avg);
Console.WriteLine();
}
Console.ReadKey();
}
#endregion

#region 验证浮点数方法
public static bool Isfloat(string Input)
{
if (Input == null)
{
return false;
}
else
{
string pattern = "^(\\d*\\.)?\\d+$";
if (Regex.Match(Input, pattern, RegexOptions.Compiled).Success)
{
return true;
}
else
{
return false;
}
}
}
#endregion
}

#endregion
改写下半生

2024-11-26 08:04:24

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestAnthing
{
class Program
{
static void Main(string[] args)
{
Student.InPutAllScore();
Student.Students.Sort();
Student.ListAllInfo();
Console.ReadLine();
}
}

// 课目枚举
public enum ClassName {
CPP, Eng, Math
}

// 单科成绩类
public class Score
{
public ClassName ItemName { get; set; } // 科目
public Decimal ScoreNum { get; set; }
}

// 学生类
public class Student: IComparable
{
public string ID { get; set; }
public string Name { get; set; }

// 下面用一个字典来保存此学生的各课成绩
public Dictionary<ClassName, Score> Score = new Dictionary<ClassName, Score>();

// 只读属性:平均分
public Decimal AvgSocre
{
get
{
var a = (from s in this.Score.Values
select s.ScoreNum).Average();
return a;
}
}

// 为学生设置一科分数
public void SetScore(ClassName c, decimal dcScore)
{
this.Score[c] = new Score() { ItemName = c, ScoreNum = dcScore };
}

// 得到学生的某科分数
public decimal GetScore(ClassName c)
{
Score s = this.Score[c];
return s == null ? 0m : s.ScoreNum;
}

// 全局保存全班学生
public static List<Student> Students = new List<Student>();

// 输入全班分数
public static void InPutAllScore()
{
int i = 0;
while (true)
{
i++;
Console.WriteLine("请输入第{0}个学生的信息, 学号输入end表示结束", i);
Console.Write("学号:");
string sID = Console.ReadLine();
if (sID == "end")
break;

Student s = new Student();
s.ID = sID;

Console.Write("姓名:");
string sName = Console.ReadLine();
s.Name = sName;

Console.Write("C++成绩:");
string sScore = Console.ReadLine();
s.SetScore(ClassName.CPP, decimal.Parse(sScore));
s.Name = sName;

Console.Write("英语成绩:");
sScore = Console.ReadLine();
s.SetScore(ClassName.Eng, decimal.Parse(sScore));
s.Name = sName;

Console.Write("数学成绩:");
sScore = Console.ReadLine();
s.SetScore(ClassName.Math, decimal.Parse(sScore));
s.Name = sName;

Console.WriteLine("此学生的平均分数为:{0}", s.AvgSocre);

Students.Add(s);
}
}

#region IComparable 成员, 用于排序

int IComparable.CompareTo(object obj)
{
Student s = obj as Student;
return -(int)(this.AvgSocre - s.AvgSocre);
}

#endregion

// 输出全部
public static void ListAllInfo()
{
Console.WriteLine("");
for (int i=0; i< Students.Count; i ++)
{
Student s = Students[i];
Console.WriteLine(
"第{0}名:{1, -10} {2, -10} C++: {3,5:#.0} 英语: {4,5:#.0} 数学: {5,5:#.0} 平均分: {6,5:#.0}",
i+1, s.ID, s.Name, s.GetScore(ClassName.CPP),
s.GetScore(ClassName.Eng), s.GetScore(ClassName.Math), s.AvgSocre);

}
}
}
}