我正在为我的计算机科学课进行测验,基本概念是你有15个关键词和15个定义。所有这些都需要随机显示,并且必须出现正确的答案。用户必须将正确的定义与关键字匹配两次,然后再次不显示该关键字和定义。当所有人都被回答两次时,测验就结束了。
我已将我的关键字和我的定义存储在同一个文件中,因此它们不会失去同步。文本文件如下所示:
Keyword1,Definition1
Keyword2,Definition2
Keyword3,Definition3
...
感谢StackOverflow上其他人的帮助,我设法完成了随机化和字典化。供参考,如下所示:
Public Class form1
Const NUMBER_OF_ANSWERS As Integer = 3
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim kv As New Dictionary(Of String, String)
For Each line As String In IO.File.ReadAllLines("C:\Users\Matt\Documents\keywords.txt")
Dim parts() As String = line.Split(",")
kv.Add(parts(0), parts(1))
Next
Dim r As New Random
Dim kvRandom As List(Of KeyValuePair(Of String, String)) =
kv.OrderBy(Function() r.Next).ToList
'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
Dim keywords As New List(Of String)
keywords.Add(keyword)
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)).OrderBy(Function() r.Next).ToList
LabelKeyword.Text = keyword
RadioButtonDef1.Text = definitionsRandom(0)
RadioButtonDef2.Text = definitionsRandom(1)
RadioButtonDef3.Text = definitionsRandom(2)
Next
End Sub
此代码完美无缺。我现在需要做的事情,但我发现很困难的是button1_click
我需要检查答案是否成功。换句话说,检查选中的定义是关键字的正确答案。如果是,则显示正确答案消息,否则显示错误消息并再次随机化。
另一个要求是每个关键字应该与其定义匹配两次,当它完成后,它将从列表中删除,不再显示。换句话说,每个问题必须完成两次,当问题不再显示时。当所有问题都被回答两次后,测验结束。我不知道如何做到这一点,所以任何帮助将不胜感激。
很抱歉,如果我很难理解我的要求,我会尽量让它变得清晰。
答案 0 :(得分:1)
部分问题是字典结构只包含两部分;在这种情况下,问题和答案。但是,需要第三部分:回答的次数。这可以通过创建Answer
类来实现,该类包含答案字符串和回答布尔值,例如:
Public Class Answer
Public Answer as String
Public Answered as Boolean
End Class
...然后:
....
Private kv As Dictionary(Of String, Answer)
Private keyword As String
Private correctDefinition As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
kv = New Dictionary(Of String, Answer)
For Each line As String In IO.File.ReadAllLines("C:\Users\Matt\Documents\keywords.txt")
Dim parts() As String = line.Split(",")
kv.Add(parts(0), New Answer with {.Answer = parts(1), .Answered = False}})
Next
RandomiseAnswers()
End Sub
kv
,keyword
和correctDefinition
必须是表单的全局,以允许与button1_click
调用的代码进行交互
首次回答问题时,请将.Answered
设置为True
,然后将其从button1_click
中的列表中删除:
If (RadioButton1.Checked and RadioButton1.Text = correctDefinition) or (RadioButton2.Checked and RadioButton2.Text = correctDefinition) or (RadioButton3.Checked and RadioButton3.Text = correctDefinition) then
If kv(keyword).Answered Then
kv.Remove(keyword)
Else
kv(keyword).Answered = True
End If
End If
RandomiseAnswers()
要防止Form1_load
和button1_click
中的重复代码,请创建子例程:
Private Sub RandomiseAnswers()
Dim r As New Random
Dim kvRandom As List(Of KeyValuePair(Of String, Answer)) =
kv.OrderBy(Function() r.Next).ToList
'questions will appear in random order
For Each line As KeyValuePair(Of String, Answer) In kvRandom
Dim keyword As String = line.Key
Dim correctDefinition As String = line.Value.Answer
Dim keywords As New List(Of String)
keywords.Add(keyword)
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)).OrderBy(Function() r.Next).ToList
LabelKeyword.Text = keyword
RadioButtonDef1.Text = definitionsRandom(0)
RadioButtonDef2.Text = definitionsRandom(1)
RadioButtonDef3.Text = definitionsRandom(2)
Next
End Sub