显示两个比较数组的结果

Displaying Result of two compared arrays

也许我只是需要走开,但我一直盯着这段代码,我不知道我需要做什么。我正在将提交的响应(在文本文件中)与已写入数组的答案键(单独的文本文件)进行比较。我使用了一个 for 循环来比较数组,我计算了正确答案和错误答案的数量。

我正在尝试显示标签中不正确的已提交答复的具体问题编号(索引编号)。我已经尝试过 for 循环、foreach 以及使用字符串和数组的各种方法。我不确定问题是否出在比较数组的原始 for 循环中,或者它是否与我尝试通过连接显示字符串的方式有关。抱歉,如果答案就在我眼前,而我错过了它。

private void buttonGrade_Click(object sender, EventArgs e)
        {
            string[] correct = new string[20];
            string[] wrong = new string[20];
            string[] result = new string[20];

            string corAns = "";
            string incorAns = "";

            StreamReader inputFile;
            StreamReader staticFile;
            inputFile = File.OpenText("Response.txt");
            staticFile = File.OpenText("Answers.txt");
            int count = 0;
            string response = "";
            string answers = "";
                        
            

            int strAcount = 0; //Counter for correct answers
            int strRcount = 0; //Counter for incorrect answers
           
            while (inputFile.EndOfStream == false && staticFile.EndOfStream == false)
            {
                response = inputFile.ReadLine();
                answers = staticFile.ReadLine();
                correct[count] = response;
                wrong[count] = answers;
                count++;
            }


            for (int j = 0; j < correct.Length; j++)
            {
                if (correct[j] == wrong[j])
                {

                    corAns += 1;
                    strAcount = corAns.Length;
                }
                else
                {
                    incorAns += 1;
                    strRcount = incorAns.Length;
                }
            }

            if (strAcount > 14)
            {
                labelOutputpassfail.Text = "Pass";
            }
            else
            {
                labelOutputpassfail.Text = "Fail";
            }

            inputFile.Close();
            staticFile.Close();

            string myString = "#s - ";
            for (int i = 0; i < 20; i += strRcount)
            {
                myString += incorAns.ToString() + ", ";
            }
            labelOutputmissed.Text = myString;
            
            labelOutputcorrect.Text = strAcount.ToString();
            labelOutputincorrect.Text = strRcount.ToString();

            
           
        }   

您需要错误答案 3、8、11.. 等的索引,但将 corAns 声明为字符串,因此它不会递增,只会连接。从您的代码中,我假设您想比较一个(提交的响应数组 - 输入文件)并且您想要根据一个(正确答案数组 - staticFile)验证它,然后生成和结果数组的不正确响应的索引。这就是我编写代码的方式:

inputFile = File.OpenText("Response.txt");
staticFile = File.OpenText("Answers.txt");
string[] response = new string[];
string[] answers = new string[];
string[] result = new string[];

foreach(string r in inputFile) {
response += r;
}
foreach(string a in staticFile) {
answers += a;
}

if(response.Length == answers.Length) {
for(int i=0; i<response.Length; i++) {
//get incorrect answer index and add to string result
if(!response[i].Equals(answers[i])) {
result += i.ToString();
   }
  }
}
return result.ToArray();

此外,如果您想要单行线,Linq 是一个不错的选择:

string[] result = response.Except(answers);