简体   繁体   English

C#数据对比

[英]C# data comparison

my task is to read data from two text files, store that data in two separate arrays, read how many of lines match, how many of the lines don't match, and display which specific lines don't match.我的任务是从两个文本文件中读取数据,将该数据存储在两个单独的 arrays 中,读取有多少行匹配,有多少行不匹配,并显示哪些特定行不匹配。 I know a for loop would be better but I can't remember what to do for it.我知道 for 循环会更好,但我不记得该怎么做。 I'm not sure of where to go from here and if anyone knows anything that could help, help would be greatly appreciated.我不确定从这里到 go 的位置,如果有人知道任何可以提供帮助的信息,将不胜感激。

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;
using System.IO;

namespace Final_Project
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public void grade_Click(object sender, EventArgs e)
        {
            string[] A = new string[20];
            string[] B = new string[20];
            StreamReader ifile;
            StreamReader ifile2;
            ifile = File.OpenText("answerkey.txt");
            ifile2 = File.OpenText("response.txt");
            int correct = 0;
            int incorrect = 0;
            string str = "";
            string str2 = "";
            while (ifile.EndOfStream == false & ifile2.EndOfStream == false)
            {
                str = ifile.ReadLine();
                str2 = ifile2.ReadLine();
            }

            A[correct] = str;
            B[incorrect] = str2;

            if (A[0] == B[0])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[1] == B[1])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[2] == B[2])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[3] == B[3])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[4] == B[4])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[5] == B[5])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[6] == B[6])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[7] == B[7])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[8] == B[8])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[9] == B[9])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[10] == B[10])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[11] == B[11])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[12] == B[12])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[13] == B[13])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[14] == B[14])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[15] == B[15])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[16] == B[16])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[17] == B[17])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[18] == B[18])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (A[19] == B[19])

            {
                correct++;
            }
            else

            {
                incorrect++;
            }

            if (correct >= 14)
            {
                passfail.Text = "Passed";
            }
            else
            {
                passfail.Text = "Failed";
            }

            string[] result = File.ReadAllLines("answerkey.txt");

            correctbox.Text = correct.ToString();
            incorrectbox.Text = incorrect.ToString();
        }
    }
}

Here are few pointers:这里有几个指针:

  1. Use File.ReadAllLines to read the files.使用File.ReadAllLines读取文件。
  2. Write a for loop that iterates over the answer file lines and compare A[i] to B[i] and update correct and incorrect .编写一个循环遍历答案文件行并将 A[i] 与 B[i] 进行比较并更新correctincorrect for循环。
  3. Think of a case where the responses file has less lines than the answers file.考虑一下响应文件的行数少于答案文件的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM