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

  • 相关阅读:
    vue3 中的路由传参
    js还原底层简单的history路由
    js还原底层简单的hash路由
    使用proxy以及Reflect还原vue3中的shallowReadonly 与 readonly响应式的原理
    flex布局兼容浏览器处理
    使用proxy以及Reflect还原vue3中的shallowReactive与reactive响应式的原理
    TS学习笔记--其他(声明文件、内置对象)
    TS学习笔记--泛型
    TS学习笔记--函数
    springboot+mybatis-plus快速精简配置
  • 原文地址:https://www.cnblogs.com/AlinaL/p/12852180.html
Copyright © 2011-2022 走看看