• 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

  • 相关阅读:
    1022. D进制的A+B (20)
    1032. 挖掘机技术哪家强(20)
    1001. 害死人不偿命的(3n+1)猜想 (15)
    结构-06. 复数四则运算(15)
    结构-05. 有理数均值(20)
    结构-04. 通讯录的录入与显示(10)
    结构-03. 平面向量加法(10)
    软考错题合集之13-05-AM
    软考笔记第八天之法律发规与标准化知识
    软考笔记第七天之程序设计语言与语言处理程序基础
  • 原文地址:https://www.cnblogs.com/AlinaL/p/12852180.html
Copyright © 2020-2023  润新知