C# 为什么要获取System.ArgumentOutOfRangeException:“minValue”不能大于maxValue。参数名称:minValue'错误?
我有一个非常简单的基本技能课程。这个问题在守则内。 我想从文本文件中加载问题 我已经在文本文件中添加了一些问题 我有一个列表索引,其中一个随机问题将从文本文件中选取,并作为标签加载 问题是,当我尝试加载问题时,我得到一个: System.ArgumentOutOfRangeException:minValue'不能大于maxValue。参数名称:minValue'错误 这是我的密码:C# 为什么要获取System.ArgumentOutOfRangeException:“minValue”不能大于maxValue。参数名称:minValue'错误?,c#,windows,visual-studio,winforms,C#,Windows,Visual Studio,Winforms,我有一个非常简单的基本技能课程。这个问题在守则内。 我想从文本文件中加载问题 我已经在文本文件中添加了一些问题 我有一个列表索引,其中一个随机问题将从文本文件中选取,并作为标签加载 问题是,当我尝试加载问题时,我得到一个: System.ArgumentOutOfRangeException:minValue'不能大于maxValue。参数名称:minValue'错误 这是我的密码: private void LoadQuestions() { if(Gl
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>();
我想把问题从文本文件放到标签上,但我得到了那个错误
有什么建议吗
谢谢 我猜你在这里摔倒了
Globals.listIndex = random.Next(0, strAnswer.Count - 1);
是斯特兰斯沃吗?数到0了吗?通过调试来检查这一点。如果不是,请确定是哪一点导致了异常?分析问题的步骤:1异常几乎总是有一个堆栈跟踪,告诉您引发异常的确切位置,以便您知道要查看哪一行代码。2逐步检查您的代码,以了解异常发生的原因。我的第一个猜测是:strAnswer.Count等于或小于0,因此random.Next0,strAnswer.Count-1;抛出此异常。为什么strAnswer.Count为0,您可以通过调试进行调查。