Arrays 读取和分析txt文件

Arrays 读取和分析txt文件,arrays,vb.net,structure,Arrays,Vb.net,Structure,我想写一个代码,其中两个txt文件正在相互比较。一个文本文件的答案类似于TFTTFFT,另一个txt文件的结果包括id类似于1234 TT-FFT。疯狂的是,对于每个正确的答案,学生得到4分,对于每个错误的答案,学生得到-1,对于每个没有答案,在这种情况下由破折号表示,学生得到零。我该如何着手为此编写代码?任何帮助都将不胜感激。我不一定需要人为我编写代码,因为我几乎已经打开了文件并将它们存储在适当的变量中。只要反馈一下如何着手做这件事就好了。先谢谢你 更新: 我已经修改了整个代码,并将其提交考虑

我想写一个代码,其中两个txt文件正在相互比较。一个文本文件的答案类似于TFTTFFT,另一个txt文件的结果包括id类似于1234 TT-FFT。疯狂的是,对于每个正确的答案,学生得到4分,对于每个错误的答案,学生得到-1,对于每个没有答案,在这种情况下由破折号表示,学生得到零。我该如何着手为此编写代码?任何帮助都将不胜感激。我不一定需要人为我编写代码,因为我几乎已经打开了文件并将它们存储在适当的变量中。只要反馈一下如何着手做这件事就好了。先谢谢你

更新: 我已经修改了整个代码,并将其提交考虑。谢谢你,乔希,也谢谢所有到目前为止做出贡献的人。请让我进一步了解你们对修改后的编码的看法


更新:程序不工作:

由于我假设您刚刚开始编程,我将分享一些建议和解决问题的一般方法

作为一名程序员,你需要掌握的最重要的技能是能够将一个复杂的问题分解成简单易懂的部分。在我看来,这个问题有三个独立的部分。我将尝试用伪代码来表示这些

读取和解析文件 将学生的答案与答案进行比较,并计算学生的分数 整个程序应该只包含几个方法。任何更复杂的事情,你都想得太多了


请记住,对您的任何程序进行此练习都会有所帮助。更好的办法是,在源文件中以注释的形式写出这些内容,然后填写必要的代码。您会惊奇地发现自己完成得如此之快。

如何插入到目前为止我编写的代码?注释框不允许我插入代码。请编辑您的问题并添加它。使用“代码格式”按钮或带有四个空格的缩进来获得正确的格式。确定完成。我使用的代码从文本文件中读取文件,并将它们存储在适当的变量中。我不确定Btnaswer程序中答案部分的阅读是否正确。你能发布这些文件前几行的简短示例吗?显然用txt文件解析输入格式是至关重要的。谢谢Josh。我相信在我的代码阅读和解析文件的过程中,第一部分是正确的,但我仍然在努力实现下一部分,即比较和计算。我会再试一次,如果我仍然不成功,我会粗暴地回到这个论坛:对不起,伙计们,我仍然很困惑。Josh我尝试了你推荐的方法,但仍然没有成功。我不确定“只包含几个方法”部分。这可能在方法上有点低,我认为,鉴于OP的明显技能水平,如果他试图坚持这个常数,他可能会得到一些繁琐、过长的方法。@ElRonnoco-你需要一个阅读答案的方法,一个阅读学生档案的方法,还有一个计算分数的方法。我的意图不是要给他施加这种约束,而是给他一个指导。如果他发现自己在写一堆代码,那么他就把问题复杂化了。追求简单是他需要学习的一项技能,需要尽早开始。今天有太多的开发人员只是简单地编写问题代码,因为他们就是这样学会的。
    Dim answers As String = 'Text from the answor file
    Dim student As String = 'Text from the student file

    ' a will be used to hold the first char from the answers file
    ' s will be used to hold the first char from the student file
    Dim a As String
    Dim s As String

    ' veriable that holds the student grad
    Dim grade As Integer = 0

    ' loop while we still have an answer
    While answers.Length > 0

        ' get the first answer and student respond
        a = answers.First
        s = student.First

        ' compare the two and update the student grade accourdingly
        If a = "-" Then
            grade = grade - 1

        ElseIf a = s Then
            grade = grade + 4
        Else
            grade = grade - 1
        End If

        ' remove the first answer from the two strings
        answers = answers.Substring(1)
        student = student.Substring(1)
    End While


    return grade
Set (Student Score) equal to '0'

FOR EACH (Answer) in (Student's Answers Array)

   Get (Key) from (Answer Key Array) at (Current Loop Index)

   IF (Answer) equals [NO_ANSWER] THEN
      Continue

   IF (Answer) equals (Key) THEN
      Add 4 to (Student Score)
   ELSE
      Subtract 1 from (Student Score)
    Dim answers As String = 'Text from the answor file
    Dim student As String = 'Text from the student file

    ' a will be used to hold the first char from the answers file
    ' s will be used to hold the first char from the student file
    Dim a As String
    Dim s As String

    ' veriable that holds the student grad
    Dim grade As Integer = 0

    ' loop while we still have an answer
    While answers.Length > 0

        ' get the first answer and student respond
        a = answers.First
        s = student.First

        ' compare the two and update the student grade accourdingly
        If a = "-" Then
            grade = grade - 1

        ElseIf a = s Then
            grade = grade + 4
        Else
            grade = grade - 1
        End If

        ' remove the first answer from the two strings
        answers = answers.Substring(1)
        student = student.Substring(1)
    End While


    return grade