c#復習題
使用泛型集合和類來做
請輸入學生個數:n
請輸入數字!!
請輸入學生個數:aa
請輸入數字!!
請輸入學生個數:
======================================
請輸入第1個學生的姓名:(不能為空)
請輸入第1個學生的性別:(只能填“男”“女”,填別的都提示錯誤!)
請輸入第1個學生的成績:(必須為0~100的數字)
======================================
請輸入第2個學生的姓名:
請輸入第2個學生的性別:
請輸入第2個學生的成績:
======================================
...
...
請輸入第n個學生的姓名:
請輸入第n個學生的性別:
請輸入第n個學生的成績:
======================================
語文成績總和為:
數學成績總和為:
英語成績總和為:
======================================
語文平均分:
數學平均分:
英語平均分:
===============成績排名================
名次 姓名 性別 語文成績 數學成績 英語成績 總分
1 ... ... 100
2 ... ... 99
3 ... ... 98
...
..
.
n
按照總分來排名次
static void Main(string[] args)
{
int stucou = 0;
while (true)
{
Console.Write("請輸入學生個數:");
string str = Console.ReadLine();
try
{
stucou = Convert.ToInt32(str);
if (stucou <= 0)
{
Console.WriteLine("請輸入正確數字!");
}
else if (stucou > 0)
{
break;
}
}
catch
{
Console.WriteLine("請輸入數字!");
}
}
List<student> slist = new List<student>();
for (int i = 1; i <= stucou; i++) {
Console.WriteLine("======================================");
student stu = new student();
while (true)
{
Console.Write("請輸入學生姓名:");
stu.name = Console.ReadLine();
try
{
if (stu.name == "" )
{
Console.WriteLine("名字不能為空");
}
else
{
break;
}
}
catch
{
break;
}
}
while (true)
{
Console.Write("請輸入學生性別:");
stu.sex = Console.ReadLine();
try
{
if (stu.sex == "男" || stu.sex == "女")
{
break;
}
else {
Console.WriteLine("請輸入男或女");
}
}
catch
{
break;
}
}
while (true)
{
Console.Write("請輸入語文成績:");
stu.chinesescore = Convert.ToDecimal(Console.ReadLine());
try
{
if (stu.chinesescore <0||stu.chinesescore >100)
{
Console.WriteLine("請輸入0-100");
}
else if (stu.chinesescore> 0&&stu.chinesescore <=100)
{
break;
}
}
catch
{
break;
}
}
while (true)
{
Console.Write("請輸入數學成績:");
stu.mathscore = Convert.ToDecimal(Console.ReadLine());
try
{
if (stu.mathscore < 0 || stu.mathscore > 100)
{
Console.WriteLine("請輸入0-100");
}
else if (stu.mathscore > 0 && stu.mathscore <= 100)
{
break;
}
}
catch
{
break;
}
}
while (true)
{
Console.Write("請輸入英語成績:");
stu.englishscore = Convert.ToDecimal(Console.ReadLine());
try
{
if (stu.englishscore < 0 || stu.englishscore > 100)
{
Console.WriteLine("請輸入0-100");
}
else if (stu.englishscore > 0 && stu.englishscore <= 100)
{
break;
}
}
catch
{
break;
}
}
stu.zongsum = stu.chinesescore + stu.mathscore + stu.englishscore;
slist.Add(stu);
}
decimal csum = 0;
decimal msum = 0;
decimal esum = 0;
decimal cvag = 0;
decimal mvag = 0;
decimal evag = 0;
decimal zongsum = 0;
foreach (student s in slist) {
csum +=s.chinesescore;
msum += s.mathscore;
esum += s.englishscore;
zongsum = s.chinesescore + s.mathscore + s.englishscore;
}
cvag = csum / stucou;
mvag = msum / stucou;
evag = esum / stucou;
Console.WriteLine("======================================");
Console.WriteLine("語文成績總和為:" + csum);
Console.WriteLine("數學成績總和為:" + msum);
Console.WriteLine("英語成績總和為:" + esum);
Console.WriteLine("======================================");
Console.WriteLine("語文平均成績為:" + cvag);
Console.WriteLine("數學平均成績為:" + mvag);
Console.WriteLine("英語平均成績為:" + evag);
for (int a = 0; a < slist.Count - 1; a++)
{
for (int b = a + 1; b < slist.Count; b++)
{
if (slist[a].zongsum < slist[b].zongsum)
{
student zhong = slist[a];
slist[a] = slist[b];
slist[b] = zhong;
}
}
}
Console.WriteLine("===============成績排名================");
Console.WriteLine("名次 姓名 性別 語文成績 數學成績 英語成績 總分");
int mc = 1;
foreach (student s in slist)
{
Console.WriteLine(mc + " " + s.name + " " + s.sex + " " + s.chinesescore + " " + s.mathscore + " " + s.englishscore + " " + s.zongsum);
mc++;
}
Console.ReadLine();
}
}
類:
class student
{
private string _name;
public string name {
get { return _name; }
set { _name = value; }
}
private string _sex;
public string sex {
get { return _sex; }
set { _sex = value; }
}
private decimal _chinesescore;
public decimal chinesescore
{
get { return _chinesescore; }
set
{
_chinesescore= value;
}
}
private decimal _mathscore;
public decimal mathscore {
get { return _mathscore; }
set { _mathscore = value; }
}
private decimal _englishscore;
public decimal englishscore {
get { return _englishscore; }
set { _englishscore = value; }
}
private decimal _zongsum;
public decimal zongsum {
get { return _zongsum; }
set { _zongsum = value; }
}
c#復習題