两个数组的哪些元素不匹配

本文关键字:元素 不匹配 数组 两个 | 更新日期: 2023-09-27 17:56:13

我有一个程序,可以根据另一个数组中定义的正确答案对文本文件中的多项选择答案进行评分。这一切都正常工作,但我想添加显示哪些问题编号不正确的功能,以便用户知道他们出错的地方。下面是我减去任何创建此功能的尝试的代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Assignment1_Part_2b
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void exitBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            string[] answers = { "B", "D", "A", "A", "C", "A", "B", "A", "C", "D", 
                                   "B", "C", "D", "A", "D", "C", "C", "B", "D", "A" };
            string[] studentAnswers = new string[20];
            studentAnswers = File.ReadAllLines("StudentAnswers.txt");
            int correct = 0;
            int incorrect = 0;
            int totalMark;

            for (int i = 0; i < answers.Length; i++)
            {
                if (answers[i] == studentAnswers[i])
                {
                    correct = (correct + 1);
                }
                else
                {
                    incorrect = (incorrect++);
                }
                totalMark = (correct - incorrect);
                wrongAnswerLabel.Text = incorrect.ToString();
                correctLabel.Text = correct.ToString();
                if (totalMark > 14)
                {
                    resultTextBox.Text = "Pass";
                }
                else
                {
                    resultTextBox.Text = "Fail";
                }

            }      
        }
        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearAll();
        }
        private void ClearAll()
        {
            resultTextBox.Text = "";
            incorrectLabel.Text = "";
        }
    }
}

我一直在思考如何处理这个问题,因此任何帮助将不胜感激。

谢谢大家。

两个数组的哪些元素不匹配

声明另一个列表来存储错误答案的索引:

var wrongAnswers = new List<int>();

然后,如果答案不正确,则将其索引添加到列表中:

else
{
    incorrect = (incorrect++);
    wrongAnswers.Add(i + 1); 
}

顺便说一句,你可以用incorrect++;而不是这个incorrect = (incorrect++);

另外,我认为您可能希望将这些行移到循环之外:

totalMark = (correct - incorrect);
wrongAnswerLabel.Text = incorrect.ToString();
correctLabel.Text = correct.ToString();
if (totalMark > 14)
{
    resultTextBox.Text = "Pass";
}
else
{
     resultTextBox.Text = "Fail";
}

为了显示错误的答案号码,您可以使用String.Join并将数字连接成单个string并像这样显示:

string text = string.Join(" ", wrongAnswers);
wrongAnswersLabel.Text = "Wrong answers: " + text;
您可以使用

Linq 单行:

public int[] WrongAnswers( char[] reference , char[] answer )
{
  if ( reference.Length != answer.Length ) throw new ArgumentOutOfRangeException();
  int[] wrongAnswers = Enumerable
                       .Range(0,reference.Length)
                       .Select( i => new Tuple<int,bool>( i , reference[i]==answer[i] ) )
                       .Where( x => !x.Item2)
                       .Select( x => x.Item1 )
                       .ToArray()
                       ;
  return wrongAnswers ;
}

你可以做这样的事情:

public int[] WrongAnswers( char[] reference , char[] answer )
{
  if ( reference.Length != answer.Length ) throw new ArgumentOutOfRangeException();
  List<int> wrongAnswers = new List<int>() ;
  for ( int i = 0 ; i < reference.Length ; +=i )
  {
    if ( reference[i] != answer[i] ) wrongAnswers.Add(i) ;
  }
  return wrongAnswers.ToArray() ;
}

您可以在循环之外定义数组或列表,并在不正确时向其添加问题编号。您最终会得到不正确的问题列表。