For Dictionary with Dictionary / List Not Counting

时间:2018-03-20 05:26:17

标签: c# list dictionary for-loop

我内心最嵌套的for循环没有正确计数。它变成了一个无限循环,我不明白为什么。是否与

有关
studentScores.Add(intScore);

在嵌套的for循环中?

class Program
{
    static void Main(string[] args)
    {
        string studentCount = string.Empty;
        string examCount = string.Empty;

        Console.WriteLine("Please enter the number of students you will enter.");
        studentCount = Console.ReadLine();
        int totalStudents = Convert.ToInt32(studentCount);

        Console.WriteLine(string.Empty);
        Console.WriteLine("Please enter the number of exams to be entered for each student.");
        examCount = Console.ReadLine();
        int totalExams = Convert.ToInt32(examCount);
        Console.WriteLine(string.Empty);

        Dictionary<int, List<int>> studentMap = new Dictionary<int, List<int>>();
        List<int> studentScores = new List<int>();

        for (int students = 0; students < totalStudents; students++)
        {
            for (int scores = 0; scores < totalExams; scores++)
            {
                string score = string.Empty;
                Console.WriteLine("Enter exam");
                score = Console.ReadLine();
                int intScore = Convert.ToInt32(score);
                studentScores.Add(intScore);
            }
        }
    }
}

3 个答案:

答案 0 :(得分:2)

您可以使用以下代码段。见here for full working code

Dictionary<int, List<int>> studentMap = new Dictionary<int, List<int>>();

for (int students = 0; students < totalStudents; students++)
{
    //Create List<int> here for each student
    List<int> studentScores = new List<int>(); 
    for (int scores = 0; scores < totalExams; scores++)
    {
       //Read and save scores of student in each subject
        string score = string.Empty;
        Console.WriteLine("Enter exam");
        score = Console.ReadLine();
        int intScore = Convert.ToInt32(score);
        studentScores.Add(intScore);
    }

    //Add this in dictonary. Key as the `index` and 
    //value as the scores saved in `studentScores`
    studentMap.Add(students, studentScores);
}

答案 1 :(得分:1)

List<int> studentScores = new List<int>();

for (int students = 0; students < totalStudents; students++)
{
    for (int scores = 0; scores < totalExams; scores++)
    {
        string score = string.Empty;
        Console.WriteLine("Enter exam");
        score = Console.ReadLine();
        int intScore = Convert.ToInt32(score);
        studentScores.Add(intScore);
    }
}

应该是:

for (int students = 0; students < totalStudents; students++)
{
    List<int> studentScores = new List<int>();
    for (int scores = 0; scores < totalExams; scores++)
    {
        string score = string.Empty;
        Console.WriteLine("Enter exam");
        score = Console.ReadLine();
        int intScore = Convert.ToInt32(score);
        studentScores.Add(intScore);
    }
    studentMap[students] = studentScores;
}

这意味着Dictionary中的每个条目都会包含studentScores数据的子集。这与您现有的代码不同,后者会将所有studentScores数据归为一个List

答案 2 :(得分:0)

根据您在评论中提供的说明,您应该尝试以下内容:

Dictionary<int, List<int>> studentMap = new Dictionary<int, List<int>>();
for (int student = 0; student < totalStudents; student++)
{
    studentMap.Add(student, new List<int>());
    for (int scores = 0; scores < totalExams; scores++)
    {
        string score = string.Empty;
        Console.WriteLine("Enter exam");
        score = Console.ReadLine();
        int intScore = Convert.ToInt32(score);
        studentMap[student].Add(intScore);
    }
}