【问题标题】:For Loop with Dictionary / List Not Counting带字典/列表的 For 循环不计数
【发布时间】:2018-08-28 20:01:31
【问题描述】:

我最内层嵌套的 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);
            }
        }
    }
}

【问题讨论】:

  • 我看不出有什么问题,您的循环将在您提供 N 个值后结束。 N 是 totalStudents * totalExams
  • 您的意思是在您的外循环中将studentScores 添加到studentMap(然后将新的List 分配给studentScores)吗?

标签: c# list dictionary for-loop


【解决方案1】:

您可以使用以下代码 sn-p。见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);
}

【讨论】:

    【解决方案2】:
    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 中。

    【讨论】:

      【解决方案3】:

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

      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);
          }
      }
      

      【讨论】:

      • 有没有办法避免重复的studentMap[student]哈希查找?
      • @mjwills:是的,您可以创建另一个列表并继续向其中添加项目
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 2022-11-17
      • 2020-10-11
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      相关资源
      最近更新 更多