码迷,mamicode.com
首页 > Windows程序 > 详细

c#复习题

时间:2017-06-29 12:44:47      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:语文   console   ima   try   msu   static   break   使用   错误   

使用泛型集合和类来做


请输入学生个数: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#复习题

标签:语文   console   ima   try   msu   static   break   使用   错误   

原文地址:http://www.cnblogs.com/yujiamin123/p/7093684.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!