當前位置:成語大全網 - 新華字典 - C# 定義全班學生成績類,包括姓名,學號,C++成績,英語成績,數學成績,平均成績

C# 定義全班學生成績類,包括姓名,學號,C++成績,英語成績,數學成績,平均成績

兄弟咱寫了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