[英]C# System.IndexOutOfRangeException error
我正在从事一个涉及创建两个数组并比较答案以确定通过或失败状态的项目。
当我尝试运行代码时,出现以下错误:
ITEC inspect.exe中发生了类型为'System.IndexOutOfRangeException'的未处理异常
附加信息:索引超出了数组的范围。
我的代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ITEC_exam
{
public partial class ITEC_exam : Form
{
public ITEC_exam()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string line;
int cnt = 0;
//List to hold question numbers of incorrect answers
List<int> incorrect = new List<int>();
//Array to store correct answers
string[] correctAnswers = { };
//Array to store answers
string[] answers = { };
//Read the files and store answers in arrays
System.IO.StreamReader correctFile = new System.IO.StreamReader("C:\\Users\\a_day\\Desktop\\Baker_Austin_c#_final\\ITECexam\\ITEC exam\\correctAnswers.txt");
System.IO.StreamReader answerFile = new System.IO.StreamReader("C:\\Users\\a_day\\Desktop\\Baker_Austin_c#_final\\ITECexam\\ITEC exam\\testResult.txt");
if ((line = correctFile.ReadLine()) != null)
correctAnswers = line.Split(' ');
if ((line = answerFile.ReadLine()) != null)
answers = line.Split(' ');
//Compare answers and compute the score
for (int i = 0; i < 21; i++)
{
if (String.Compare(correctAnswers[i], answers[i]) == 0)
cnt++;
else
incorrect.Add(i + 1);
}
//Print Result
if (cnt >= 15)
Console.WriteLine("\n\n Result: PASS");
else
Console.WriteLine("\n\n Result: FAIL");
//Printing score
Console.WriteLine("\n Total number of Correct Answers: " + cnt);
Console.WriteLine("\n Total number of Incorrect Answers: " + (20 - cnt));
Console.Write("\n Question numbers of incorrect answers: ");
//Printing incorrectly answered question numbers
foreach (int qno in incorrect)
Console.Write(" " + qno + " ");
//Closing Files
correctFile.Close();
answerFile.Close();
Console.WriteLine("\n\n Press any key to exit.");
Console.ReadKey();
}
}
}
您的for循环假定两个列表中都有20个或更多的项目,如果您在任何一个列表中都没有20个项目,则correctAnswers[i], answers[i]
会导致correctAnswers[i], answers[i]
失败。 在通过索引访问之前进行验证。
我假设您在答案中有一些标志,例如NA
,它不在未尝试的问题/未回答的问题的实际答案列表中
不要为未尝试的问题/未回答的问题留空或空格,它会在按空格分隔后在答案列表的结果数组中生成更多项。
另外,如果您有多个单词的答案,则此方法将失败。 (因为您被空间分割了)
if(correctAnswers.Length == answers.Length)
{
for (int i = 0; i < correctAnswers.Length; i++)
{
if (String.Compare(correctAnswers[i], answers[i]) == 0)
cnt++;
else
incorrect.Add(i + 1);
}
}
其他选项:您可以创建Answer类并添加一些属性,例如Question ID,Answer,Marks等。然后您可以从此类创建答案列表,并将整个列表保存到文件中,并使用序列化和从文件中读回列表反序列化
检查对象写入文件列表
您正在遍历大小为20的数组并访问第20个索引处的值。 检查您的循环。 它应该访问索引0到19,而不是0到20。
不应使用“ 21”进行计数, answers
可能为空或小于21。
所以使用如下。
//Compare answers and compute the score
for (int i = 0; i < answers.Length && i < correctAnswers.Length; i++)
{
if (String.Compare(correctAnswers[i], answers[i]) == 0)
cnt++;
else
incorrect.Add(i + 1);
}
您需要按以下方式检查数组以避免异常。
//Compare answers and compute the score
for (int i = 0; i < correctAnswers.Count(); i++)
{
if (answers.Count() > i && String.Compare(correctAnswers[i], answers[i]) == 0))
cnt++;
else
incorrect.Add(i + 1);
}
for (int i = 0; i < 21; i++)
{
if (i < correctAnswers.Length && i < answers.Length)
{
if (String.Compare(correctAnswers[i], answers[i]) == 0)
cnt++;
else
incorrect.Add(i + 1);
}
else
incorrect.Add(i + 1);
}
在for循环中,首先检查两个数组的索引为i
然后检查条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.