zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    杨玲 201771010133《面向对象程序设计(java)》第三周学习总结
    杨玲 201771010133《面向对象程序设计(java)》第二周学习总结
    杨玲 201771010133 《面向对象程序设计(java)》第一周学习总结
    bzoj1010 [HNOI2008]玩具装箱toy
    hdu5115 Dire Wolf
    bzoj2880
    bzoj2301 [HAOI2011]Problem b
    bzoj2440 [中山市选2011]完全平方数
    bzoj4448 情报传递
    bzoj4445 小凸想跑步
  • 原文地址:https://www.cnblogs.com/AlinaL/p/12852180.html
Copyright © 2011-2022 走看看