如何刷新测验中的文字?

时间:2014-03-05 20:48:40

标签: vb.net

我正在为我的计算机科学课进行测验,基本概念是你有15个关键词和15个定义。所有这些都需要随机显示,并且必须出现正确的答案。用户必须将正确的定义与关键字匹配两次,然后再次不显示该关键字和定义。当所有人都被回答两次时,测验就结束了。

我已将我的关键字和我的定义存储在同一个文件中,因此它们不会失去同步。文本文件如下所示:

Keyword1,Definition1
Keyword2,Definition2
Keyword3,Definition3
...

我的代码完美无缺。这是我的代码:

答案类:

Public Class Answer
Public Answer As String
Public Answered As Boolean

End Class

我的主要表格(抱歉评论,仅供我记住):

Public Class Form1
Private kv As Dictionary(Of String, Answer) 'Define kv as a dictionary
Private keyword As String
Private correctDefinition As String
Const NUMBER_OF_ANSWERS As Integer = 3 'Define the constant NUMBER_OF_ANSWERS




Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'The text file is structured like so:
    'Keyword1,Definition1
    'Keyword2,Definition2
    'Keyword3,Defintion3
    '...
    'This makes sure that the keywords and the definitions do not get out of sync when randomising, as a dictionary is a key-value data structure. Call the key, get the right value etc.
    kv = New Dictionary(Of String, Answer) 'kv = a new dictionary
    For Each line As String In IO.File.ReadAllLines("C:\Users\Matt\Documents\keywords.txt") 'Foreach loop populating the dictionary from the text file
        Dim parts() As String = line.Split(",") 'Split the Keywords from the definitions where the , is
        kv.Add(parts(0), New Answer With {.Answer = parts(1), .Answered = False}) 'Add the two parts (Keyword and Definition) to the Parts with the Answer class
    Next

    Dim r As New Random 'Define r as new random
    Dim kvRandom As List(Of KeyValuePair(Of String, String)) =
      kv.OrderBy(Function() r.Next) _
.Select(Function(x) New KeyValuePair(Of String, String)(x.Key, x.Value.Answer)) _
.ToList() 'A Select method to convert the items in the list properly

    'questions will appear in random order
    For Each line As KeyValuePair(Of String, String) In kvRandom
        Dim keyword As String = line.Key
        Dim correctDefinition As String = line.Value 'Checks that the correct definition will be displayed when its keyword is randomly displayed.

        'Define keywords as a new list
        Dim keywords As New List(Of String)
        keywords.Add(keyword) 'Adds the keyword to the list as the Part(0) from the text file in the first loop
        keywords.AddRange(kv.Keys.Except({keyword}).
          OrderBy(Function() r.Next).Take(NUMBER_OF_ANSWERS - 1))

        Dim definitionsRandom As List(Of String) =
          keywords.Select(Function(x) kv(x).Answer).OrderBy(Function() r.Next).ToList 'Define definitionsRandom as a list. Checks against correctDefinition to know to always display the correct answer as one of the 
        'random definitions

        LabelKeyword.Text = keyword 'Randomly display a keyword
        RadioButtonDef1.Text = definitionsRandom(0) 'Randomly display a definition. Checks against correctDefinition to see make sure one definition is always the right answer.
        RadioButtonDef2.Text = definitionsRandom(1) 'Randomly display a definition. Checks against correctDefinition to see make sure one definition is always the right answer.
        RadioButtonDef3.Text = definitionsRandom(2) 'Randomly display a definition. Checks against correctDefinition to see make sure one definition is always the right answer.
    Next

End Sub

Private Sub ButtonNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNext.Click
    If (RadioButtonDef1.Checked And RadioButtonDef1.Text = correctDefinition) Or (RadioButtonDef2.Checked And RadioButtonDef2.Text = correctDefinition) Or (RadioButtonDef3.Checked And RadioButtonDef3.Text = correctDefinition) Then
        If kv(keyword).Answered Then
            kv.Remove(keyword)
        Else
            kv(keyword).Answered = True
        End If
    End If
End Sub
End Class

我遇到的问题是,当用户正确回答问题时,我需要将关键字和定义再次随机化。但是,当回答正确的定义时,它保持不变。

那我怎样才能让它在buttonNext_click上再次随机化呢?目标是让用户将每个关键字与其正确的定义匹配两次,以便测验结束。

我要问的是什么(更清楚):

  1. 我如何才能使关键字和定义在buttonNext_click上再次随机化,如果答案错误告诉他们错误,则显示一个消息框,如果答案是正确的,则显示消息框告诉他们是正确的。我希望当他们点击消息框OK按钮时有下一个随机。
  2. 我不想重新加载表单,因为用户必须将每个关键字与其定义匹配两次我跟着Answer类跟踪,当没有问题时,测验结束。

    非常感谢任何帮助。

0 个答案:

没有答案