求C#大神解决一个问题,苦恼好久了!
1,写一个学生类 属性有 姓名 年龄 语数英 三科成绩
2,将10个学生对象(属性可以随机或者自己输入)存入字典中 按学习成绩顺序
3, 输出10个学生有最高分到最低分的信息 展开
2019-08-15
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StudentManager
{
public class GetRandom
{
private static Random ran = new Random();
public static double getRandom()
{
double result = 0;
result = ran.NextDouble() * 100 + 1;
result = result > 100 ? 100 : result;
result = double.Parse(result.ToString("0.0"));//保留两位有效数字
return result;
}
}
public class student
{
public student(string name, int age)
{
this.name = name;
this.age = age;
//自动生成成绩
this.chineseScore = GetRandom.getRandom();
this.mathScore = GetRandom.getRandom();
this.englishScore = GetRandom.getRandom();
}
public string name { get; set; }
public int age { get; set; }
public double chineseScore { get; set; }
public double mathScore { get; set; }
public double englishScore { get; set; }
public double getTotalScore()
{
return chineseScore + mathScore + englishScore;
}
}
class Program
{
public static Dictionary<int, student> StdDic = new Dictionary<int, student>();
static void Main(string[] args)
{
List<student> stdList = new List<student>();
stdList.Add(new student("张三蛋", 16));
stdList.Add(new student("李四狗", 16));
stdList.Add(new student("王五迟", 16));
stdList.Add(new student("赵六子", 16));
stdList.Add(new student("冯宝宝", 16));
stdList.Add(new student("钱二吗", 16));
stdList.Add(new student("无名氏", 16));
stdList.Add(new student("随便起", 16));
stdList.Add(new student("刘吵吵", 16));
stdList.Add(new student("黄伯伯", 16));
int index = 1;
while (stdList.Count > 1)
{
student temp = stdList[0];
for (int i = 1; i < stdList.Count; i++)
{
if (stdList[i].getTotalScore() < temp.getTotalScore())//升序
{
temp = stdList[i];
}
}
StdDic.Add(index, temp);
index++;
stdList.Remove(temp);
}
StdDic.Add(index, stdList[0]);
foreach (var item in StdDic)
{
Console.WriteLine(item.Value.name + ":" + item.Value.getTotalScore());//打印信息
}
Console.ReadLine();
}
}
}
自己运行看看吧