我正在为我的计算机科学课进行测验,基本概念是你有15个关键词和15个定义。所有这些都需要随机显示,并且必须出现正确的答案。用户必须将正确的定义与关键字匹配两次,然后再次不显示该关键字和定义。当所有人都被回答两次后,测验就结束了。
我已将我的关键字和我的定义存储在同一个文件中,因此它们不会失去同步。文本文件如下所示:
Keyword1 = Definition1
Keyword2 = Definition2
Keyword3 = Definition3
等(共15个)
我的主要表单如下:
Public Class quiz
Private Sub quiz_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myBase.Load
Dim MyList As List(Of KeyValuePair(Of String, String)) = New List(Of String, String))
For Each line As String In System.IO.File.ReadAllLines("my-file-path")
Dim Pair() As String = line.split("=")
mylist.add(New KeyValuePair(Of String, String)(Pair(0), Pair(1)))
Next
我在标签中显示random关键字,在radiobuttons中显示定义。两个必须是随机定义,一个必须是所显示的关键字的正确定义,也需要随机显示。
我要问的是:
答案 0 :(得分:1)
这应该会给你一个想法:
Const NUMBER_OF_ANSWERS As Integer = 3
Dim kv As New Dictionary(Of String, String)
kv.Add("Keyword1", "Definition1")
kv.Add("Keyword2", "Definition2")
kv.Add("Keyword3", "Definition3")
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
'TODO: need to write some code here
'display keyword and three possible definitions to the user
'(out of which one is correct)
'answers will also appear in random order
'Check answer against value stored in "correctDefinition"
Next
代码几乎不言自明,如果您有任何疑问,请在评论中告诉我。
编辑:以下是如何从文件中填充字典。
'assuming file structure is like this:
'keyword1,definition1
'keyword2,definition2
'keyword3,definition3
'...
For Each line As String In IO.File.ReadAllLines("keywords_and_definitions.txt")
Dim parts() As String = line.Split(",")
kv.Add(parts(0), parts(1))
Next
答案 1 :(得分:0)
在确定加载问题后,您可以按照此基本算法执行其余任务。我不想给你一个类分配的确切代码。弄清楚这种事情是学习编程的一半乐趣。