簡體   English   中英

兩個數組的哪些元素不匹配

[英]Which elements of the two arrays do not match

我有一個程序,可以將文本文件中的多項選擇答案與另一個數組中定義的正確答案進行評分。 一切正常,但我想添加功能以顯示哪些問題編號不正確,以便用戶知道他們出了錯。 以下是我嘗試創建該功能的所有代碼。

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() ;
}

您可以在循環外定義數組或列表,並在錯誤時將問題編號添加到其中。 您最終將得到不正確問題的列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM