- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习 C# 字典,但我很困惑该怎么做
我有一本学生姓名词典。我想为它分配另一本字典。
我的输入类似于此
学生 1: 数学,9 科学,5 英文,2
学生 2: 数学,9 科学,10 英文,7
我试图创建一个名为 Info 的类,这是我的代码
public class Info
{
public string course { get; set; }
public int grade { get; set; }
public Info(string c, int g)
{
course = c;
grade = g;
}
internal IDictionary<string, Info> infoDic { get; private set; }
public void Set(string Sname, string course, int grade)
{
Student s = new Student(Sname);
var infor = new Info(course, grade);
infoDic = new Dictionary<string, Info>();
infoDic.Add(s.Name, infor);
//return infoDic;
}
public Dictionary<string, Info> retrieve (string name)
{
Student s = new Student(name);
return infoDic;
}
}
}
这是另一种尝试:
我尝试制作 Info 类,主要是创建 Dictionary 并给出值,但问题是我有 3 门类(class)和 10 名学生,有时我只需要检索所有类(class)的数学成绩学生。
如何改进代码区分类(class)?或者如何将类(class)名称作为另一个键?
public class Info
{
public string course { get; set; }
public int grade { get; set; }
public Info(string c, int g)
{
course = c;
grade = g;
}
}
class Test
{
public static void Main(string[] args)
{
Dictionary<string, Info> information = new Dictionary<string, Info>();
Info i1 = new Info("math", 9);
information.Add("Student1", i1);
Info i2 = new Info("science", 11);
information.Add("Student1", i2);
Info i3 = new Info("math", 13);
information.Add("student2", i3);
foreach (KeyValuePair<string, Info> eee in information)
{
Console.WriteLine("{0}\t{1}\t{2}", eee.Key, eee.Value.type, eee.Value.count);
}
}
}
我需要两种方法,一种是设置用户输入的值,另一种是在用户需要时检索特定的类(class)值
有什么建议吗?
最佳答案
将问题拆分成单独的关注点。
使用 Student
和 Info
类只是为了存储数据。重要的是,每个学生都拥有自己类(class)的集合。
public class Student {
public Student(string name) {
Name = name;
Infos = new List<Info>();
}
public string Name {get; set;}
public ICollection<Info> Infos {get; set;}
}
public class Info {
public Info(string course, int grade) {
Course = course;
Grade = grade;
}
public string Course { get; set; }
public int Grade { get; set; }
}
数据访问由不同的类处理 StudentRepository
.中央字典的类型是 IDictionary<string, Student>
以学生姓名为键并隐藏在存储库中。
using System.Linq;
public class StudentRepository {
public StudentRepository() {
_studentsByName = new Dictionary<string, Student>();
}
// keep data store private so we can change the implementation
private IDictionary<string, Student> _studentsByName {get; set;}
public void Add(Student student) {
if (_studentsByName.ContainsKey(student.Name)) {
throw new ArgumentException($"Student '{student.Name}' already stored.");
}
_studentsByName.Add(student.Name, student);
}
public Student Get(string studentName) {
if (_studentsByName.ContainsKey(studentName)) {
return _studentsByName[studentName];
}
throw new ArgumentException("No student '" + studentName + "' stored.");
}
// Find Grade for certain student and course
public int GetGrade(string studentName, string course) {
if (_studentsByName.ContainsKey(studentName)) {
var student = _studentsByName[studentName];
var courseInfo = student.Infos.FirstOrDefault(i => i.Course == course);
if (courseInfo != null) {
return courseInfo.Grade;
}
else {
throw new ArgumentException(
$"Student '{studentName}' did not take the course '{course}'.");
}
}
else {
throw new ArgumentException($"No student '{studentName}' found.");
}
}
// Get dictionary of all students that took a certain course. Key: student name
public IDictionary<string, Info> GetCoursesByStudentName(string course) {
// Use LINQ to retrieve the infos you need.
// Here I create a new dictionary with Student name as Key and
// the first matching course info found as value.
// (Students that did not take this course are not in this dictionary):
return _studentsByName
.Where(kvp => kvp.Value.Infos.Any(i => i.Course == course))
.ToDictionary(kvp => kvp.Key,
kvp => kvp.Value.Infos.First(i => i.Course == course));
}
}
使用示例:
const string MathCourseName = "Math";
var Student1 = new Student("Alice");
Student1.Infos.Add(new Info(MathCourseName, 4));
var Student2 = new Student("Bob");
Student2.Infos.Add(new Info(MathCourseName, 2));
var Student3 = new Student("Cesar");
Student3.Infos.Add(new Info("English", 3));
var repository = new StudentRepository();
repository.Add(Student1);
repository.Add(Student2);
repository.Add(Student3);
foreach(var kvp in repository.GetCoursesByStudentName(MathCourseName)) {
Console.WriteLine(kvp.Key + ": " + kvp.Value.Course + " - " + kvp.Value.Grade);
}
var bobsMathGrade = repository.GetGrade("Bob", MathCourseName);
Console.WriteLine("Bobs math grade: " + bobsMathGrade);
关于c# - 如何为学生和多个类(class)成绩创建字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47567171/
在parser.h中: typedef enum _SEX {MALE = 'M', FEMALE = 'F', OTHER = 'O'} SEX; struct course { char gra
共有三个实体: School, teacher and student 有一些规则: 一个学生只能属于一位老师 一名学生只能属于一所学校 一名教师可以属于一所或多所学校 这意味着我的表中有这些关系:
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 2 年前。 Improve this qu
我正在构建一个涉及学生和顾问的 Web 应用程序。学生将选择他的独立学习,顾问将对其进行监督。我正在努力为我的程序找到正确的关系。 基本上我的应用程序中的参与者是。 a Student can sel
支持Python的IDE有IPython、Aptana Studio(在Eclipse的基础上加插件集改的)、PyCharm(由 JetBrains 打造的一款 Python IDE,支持 macO
我对 sql 和 Stack Overflow 非常陌生。我希望有人可以帮助我解决这个问题。我的查询应该显示销售额超过 200,000 美元的类别的总销售额和已售商品总数。我已经研究这个查询一个小时了
public class Menu extends Activity { /** Called when the activity is first created. */ public
当我在 R 中运行学生 t-分布时,我得到以下结果: > pt(1.849, 2, lower.tail=FALSE) [1] 0.1028493 当我运行 Apache Commons Math 库
我的机器人实验室正在寻找程序员来从事我们目前的一些项目。 我们确定了要求(主要是 C++ 和 openGL 和 3D 经验),但由于明显的资金限制,我们无力聘请优秀的开发人员。相反,我们将满足于有才华
我正在努力完成一个使用替换密码来编码和解码 txt 文件的程序。经过相当多的工作,我的程序几乎完成了。该程序首先询问用户是否要对文件进行编码或解码。回答这个问题后,程序会提示用户输入正在输入的文件的名
我是一个刚开始编程的菜鸟,我想知道为什么我会收到一条错误消息:非静态数据成员“Lavirint::n”的使用无效? class Lavirint{ private: int n, m;
该项目的目标是创建四个类:Student 类、GradStudent 类、Manager 类和 GUI 类。 GUI 中有两个单选按钮:一个用于学生,一个用于研究生。根据选择的对象,Manager 类
我正在尝试向学生、教师和家长 Activity 实现抽屉导航 Activity ,但它不起作用,请指导我如何在登录后向不同角色的用户添加抽屉导航。 最佳答案 您需要在 中自定义布局像这样:
我是 JS 的新手,甚至是 Jquery 和 fullcalendar 的新手。我创建了一个单独在页面上运行良好的日历。我正在尝试将此代码与另一个页面结合使用,但我无法让日历呈现。 首先我包含一些文件
我正在开发一个简单的 Linux Shell,它可以 fork() 并调用 execvp(),但我最近添加了不需要 fork() 的“内置函数”。 这是执行处理: if (strcmp(cmd
我在使用分配给我的这个初学者 Java 程序时遇到了麻烦,我是 Java 的新手,而且我在使用这个特定程序时遇到了很多麻烦。这些是说明: Your program should prompt user
在下面的代码块中,有几个(故意的)错误,我的任务是找到它们并解释这些错误是否会导致编译代码时出现问题,或者至少会导致一些逻辑问题。 public class Person { private St
我想加载一个文件,将其内容拆分为数组,然后将类应用到内容。 class Student def initialize( name, grade ) @name = name
我有 3 个实体:导师、学生、类(class) 导师教授许多类(class) 学生可以被分配到由多名导师教授的许多类(class) 导师和学生需要能够登录系统 我想使用下表来表示此设计: users
我想弄清楚这里的填充是否可调 这是 HTML: {block:IfHeaderImage} {/block:IfHeaderImage} {block:IfNot
我是一名优秀的程序员,十分优秀!