我有一个非常简单的基本技能课程。问题在代码内。 我希望从文本文件中加载问题。
我已经在文本文件中添加了一些问题。
我有一个列表索引,将从文本文件中选择一个随机问题并将其作为标签加载。
问题是当我尝试加载问题时,我得到了: System.ArgumentOutOfRangeException:“ minValue”不能大于maxValue。参数名称:minValue'错误
这是我的代码:
private void LoadQuestions()
{
if(Globals.intQuestionNumber == 11)
{
MessageBox.Show("Quiz complete - Redirecting", "Quiz Complete");
var fileStream = new FileStream(@"H:\(4)Programming\Assignments\EssentialSkills - Optimised\EssentialSkills\Numeracy\QuestionsLvl0.txt", FileMode.Open, FileAccess.Read);
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
{
string line;
while ((line = streamReader.ReadLine()) != null)
{
stringAllInfo.Add(line);
MessageBox.Show(line);
}
}
char[] delimiterChars = { ',' };
foreach (string l in stringAllInfo)
{
string[] words = l.Split(delimiterChars);
strQuestion.Add(words[0]);
strAnswer.Add(words[1]);
Console.WriteLine(words[0]);
Console.WriteLine(words[1]);
}
Menus.Summary sum = new Menus.Summary();
sum.Show();
this.Hide();
}
else
{
lblCountdownval.Visible = false;
lblCountdown.Visible = false;
Globals.intQuestionNumber += 0;
lblQuestionsNumber.Text = "Question Number: " + Globals.intQuestionNumber.ToString();
Random random = new Random();
Globals.listIndex = random.Next(0, strAnswer.Count - 1);
lblQuestion.Text = strQuestion.ElementAt(Globals.listIndex);
Globals.listQuestonsAsked.Add(strQuestion.ElementAt(Globals.listIndex));
btnCorrect.Text = strAnswer.ElementAt(Globals.listIndex).ToString();
btnAnswer1.Text = random.Next(200).ToString();
btnAnswer3.Text = random.Next(200).ToString();
int locationIndex = random.Next(0, 3);
btnCorrect.Location = points.ElementAt(locationIndex);
locationIndex = random.Next(0, 3);
btnAnswer1.Location = points.ElementAt(locationIndex);
while ((btnAnswer1.Location == btnCorrect.Location))
{
locationIndex = random.Next(0, 3);
btnAnswer1.Location = points.ElementAt(locationIndex);
}
locationIndex = random.Next(0, 3);
btnAnswer3.Location = points.ElementAt(locationIndex);
while ((btnAnswer3.Location == btnCorrect.Location) || (btnAnswer3.Location == btnAnswer1.Location))
{
locationIndex = random.Next(0, 3);
btnAnswer3.Location = points.ElementAt(locationIndex);
}
btnAnswer1.Show();
btnCorrect.BackColor = Color.White;
btnAnswer3.Show();
}
}
这是我的列表:
public List<string> strQuestion = new List<string>();
public List<string> strAnswer = new List<string>();
public List<string> stringAllInfo = new List<string>();
我想要从文本文件到标签的问题,但出现此错误。
有什么建议吗?
谢谢。
答案 0 :(得分:0)
我认为你在这里跌倒了
Globals.listIndex = random.Next(0, strAnswer.Count - 1);
strAnswer.Count是0吗?通过调试检查。如果不能,请确定导致异常的原因是什么?