【发布时间】: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