1

我试图编写一个代码,其中两个 txt 文件相互比较。一个文本文件的答案类似于“TFTTFFT”,而另一个 txt 文件的结果包括像“1234 TT-FFT”这样的 id得到-1,对于每个没有答案,在这种情况下由破折号(-)表示,学生得到零。我该如何为此编写代码?任何帮助将不胜感激。我不一定需要有人为我编写代码,因为我已经完成了文件的打开并将它们存储在适当的变量中。只是关于如何去做这件事的反馈会很好。先感谢您。

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

更新:程序不工作:(

4

2 回答 2

0

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

作为一名程序员,你需要培养的最重要的技能是能够将一个复杂的问题分解成简单且易于消化的块。正如我所看到的,这个问题有 3 个独立的部分。我会尝试用伪代码来表达这些。

读取和解析文件

Open (Answer Key File)

Read All Text From (Answer Key File) into (String)

Convert (String) into array of characters as (Answer Key Array)

Close (Answer Key File)

Open (Student's File)

Read All Text From (Student's File) into (String)

Extract (Student ID) from (String)

Extract (Student's Answers) from (String)

Convert (Student's Answers) to Character Array as (Student Answers Array)

Close (Student's File)

比较学生的答案并计算学生的分数

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)

你的整个程序应该只包含几个方法。任何更复杂的事情,你都想多了;)

请记住,对您的任何程序进行此练习都会有所帮助。更好的是,将这些作为注释写在源文件中,然后填写必要的代码。你会惊讶于你完成的速度有多快。

于 2012-10-21T01:57:12.583 回答
0
    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
于 2012-10-21T07:50:15.603 回答