我有一个问题,我基本上知道答案。我想知道为什么新创建的List<int>
是空的,为什么即使我用值填充了它也是空的?我可以很容易地在类Vysvedceni(ReportCard)
列表中创建,然后在Main类中创建一个单独的字段,然后将其添加到Dictionary<Enum,List<int>>
。。。
类似的东西:
List<Enum,List<int>> array = new ...
array.Add(1);
array.Add(2);
array.Add(5);
dic[Subject].Add(array);
但我想用不同的方式:
Student student = new Student("Jan", "Novak",new Vysvedceni());
student.Vysvedceni.Znamky.Add(Vysvedceni.Subjects.math, new List<int>() {1,1,2,3,4,3});
我的目标很简单,用学生和他/她的分数打印字典
我的母语不是英语,我想翻译代码,但如果你复制代码,我恐怕会把它弄得一团糟。至少我添加了一个小翻译。我还添加了我的评论,问题在哪里,或者用什么方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cviceni1412
{
class Student
{
private string jmeno; //Name
private string prijmeni; //Surname
private Vysvedceni vysvedceni;
//Dictionary<List<Student>, List<Vysvedceni>> vysvedceniTridy = new Dictionary<List<Student>, List<Vysvedceni>>();
List<Student> studenti = new List<Student>();
public void SetridStudenty()
{
var setrid = (from student in studenti select student)
.GroupBy(student => student.Jmeno.First()).ToList();
foreach (var item in setrid)
{
Console.WriteLine(item);
}
}
public override string ToString()
{
return "jmeno:"+jmeno + ",prijmeni:" + prijmeni;
}
public Student(string jmeno, string prijmeni, Vysvedceni vysvedceni)
{
this.jmeno = jmeno;
this.prijmeni = prijmeni;
this.vysvedceni = vysvedceni;
}
public string Jmeno { get => jmeno; set => jmeno = value; }
public string Prijmeni { get => prijmeni; set => prijmeni = value; }
internal Vysvedceni Vysvedceni { get => vysvedceni; set => vysvedceni = value; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace Cviceni1412
{
class Vysvedceni
{
private Dictionary<Enum, List<int>> znamky = new Dictionary<Enum, List<int>>();
private Dictionary<Enum, int> vizo = new Dictionary<Enum, int>();
private Predmety skolniPredmety;
public Dictionary<Enum, List<int>> Znamky { get => znamky; set => znamky = value; }
internal Predmety SkolniPredmety { get => skolniPredmety; set => skolniPredmety = value; }
public Dictionary<Enum, int> Vizo{ get => vizo; set => vizo= value; }
public double Prumer(Enum predmet) //Method for calulating Average and final mark for student
{
double prumerZnamka=Znamky[predmet].Average();
return prumerZnamka ;
}
public void Vypis()//Print List method
{
Znamky.Select(i => $"{i.Key}:{i.Value}").ToList().ForEach(Console.WriteLine);
}
public Vysvedceni()
{
}
public enum Predmety//Subjects like math,Engl,P.E.,Physic
{
matematika,cestina,fyzika,programovani,telocvik,databaze,webovky
}
}
}
//Student class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cviceni1412
{
class Student
{
private string jmeno;
private string prijmeni;
private Vysvedceni vysvedceni;
//Dictionary<List<Student>, List<Vysvedceni>> vysvedceniTridy = new Dictionary<List<Student>, List<Vysvedceni>>();
List<Student> studenti = new List<Student>();
public void SetridStudenty()//Sort students to the group by their first letter
{
var setrid = (from student in studenti select student)
.GroupBy(student => student.Jmeno.First()).ToList();
foreach (var item in setrid)
{
Console.WriteLine(item);
}
}
public override string ToString()
{
return "jmeno:"+jmeno + ",prijmeni:" + prijmeni;
}
public Student(string jmeno, string prijmeni, Vysvedceni vysvedceni)
{
this.jmeno = jmeno;
this.prijmeni = prijmeni;
this.vysvedceni = vysvedceni;
}
public string Jmeno { get => jmeno; set => jmeno = value; }
public string Prijmeni { get => prijmeni; set => prijmeni = value; }
internal Vysvedceni Vysvedceni { get => vysvedceni; set => vysvedceni = value; }
}
}
//Main class
using System;
using System.Collections.Generic;
using System.Linq;
namespace Cviceni1412
{
class Program
{
static void Main(string[] args)
{
Student student = new Student("Jan","Novák",new Vysvedceni());
student.Vysvedceni.Znamky.Add(Vysvedceni.Predmety.matematika, new List<int>() {1,1,2,3,4,3 });//There is problem,List is declared and filled,but it prints just only Subject and [System.Generics...]
Console.WriteLine(student.Vysvedceni.Znamky.Keys.Count);
student.Vysvedceni.Vypis();
}
}
}
我是如何解决的。问题不是ToString((方法,而是Print((方法。林克只重复了Keys。希望我能帮助
public void Print(Subjects sub)
{
foreach (var item in marks//dictionary)
{
Console.WriteLine(item.Key);
foreach (var ite in marks[sub])
{
Console.WriteLine(ite);
}
}
}