C#学习笔记之——学生信息输入系统(Dictionary)

创建一个学生类

public class Student
{
	protected string name;
	protected string stuID;
	protected int score;
	public Student(){
	}
	public string Name{
		set{
			if (name == null)
				name = value;
		}
		get{
			return name;
		}
	}
	public string StuID{
		set{
			if (stuID == null)
				stuID = value;
		}
		get{
			return stuID;
		}
	}
	public int Score{
		set{
			score = value;
		}
		get{
			return score;
		}
	}
}

创建dictionary存放学生信息,录入完毕用学生学号排序

Dictionary<int, Student> studentInfo = new Dictionary<int, Student> ();
//Student stu = new Student ();
Console.WriteLine ("please write down the student's information:(when write down " " (space) ,shut down)");
Console.Write ("No.");
int i;
string a = Console.ReadLine ();
while (a != " ") {
				
	Student stu = new Student ();
	Console.Write ("student's name:");
	stu.Name = Console.ReadLine ();
	Console.Write ("student's ID:");
	stu.StuID = Console.ReadLine ();
	Console.Write ("student's score:");
	stu.Score = int.Parse (Console.ReadLine ());
	i = int.Parse (a);
	studentInfo.Add (i, stu);
	Console.Write ("No.");
	a = Console.ReadLine ();
}

Dictionary<int, Student> studentIndo_SortedByValue = studentInfo.OrderBy(p=>p.Value.StuID).ToDictionary(p => p.Key, o => o.Value);
foreach (var item in studentIndo_SortedByValue) {
	Console.WriteLine (item.Key + " " + item.Value.Name + " " + item.Value.StuID + " " + item.Value.Score);
}

结果:

please write down the student's information:(when write down " " (space) ,shut down)

No.1

student's name:Pink

student's ID:03

student's score:89

No.2

student's name:Sia

student's ID:01

student's score:67

No.3

student's name:Ed

student's ID:02

student's score:45

No. 

2 Sia 01 67

3 Ed 02 45

1 Pink 03 89


黄老板的粉不要打我,我也是随便举的例子QAQ

原文地址:https://www.cnblogs.com/AlinaL/p/12852180.html