如何建立这个列表并将其随机化? [英] How to build up this list and randomize it?

查看:19
本文介绍了如何建立这个列表并将其随机化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的计算机科学课做一个测验,基本概念是你有 15 个关键字和 15 个定义.所有都需要随机显示,并且必须出现正确答案.用户必须将正确的定义与关键字匹配两次,然后才不会再次显示该关键字和定义.全部回答两次后,测验结束.

I am making a quiz for my computer science class and the basic concept is that you have 15 keywords and 15 definitions. All need to be randomly displayed and the correct answer has to appear. The user has to match the correct definition to the keyword twice and then that keyword and definition are not displayed again. When all have been answered twice the quiz is over.

我已将我的关键字和我的定义存储在同一个文件中,以免它们不同步.文本文件如下所示:

I have stored both my keywords and my definitions in the same file so they don't get out of sync. The text file looks like so:

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

我在标签中显示随机关键字,在单选按钮中显示定义.两个需要随机定义,一个必须是对显示的关键字的正确定义,也需要随机显示.

I am displaying the random keyword in a label and the definitions in radiobuttons. Two need to be random definitions and one has to be the correct definition to the keyword shown, which also needs to be displayed randomly.

我想问的是:

  1. 如何完成此列表,因为它仅使用最后一行覆盖了其他 15 行?
  2. 如何随机化显示关键字和定义的列表?
  3. 当每个关键字与其定义匹配两次时,如何删除项目?例如:关键字 1 和定义 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

代码是不言自明的,如果您有任何问题,请在评论中告诉我.

The code is pretty much self-explanatory, if you have any questions, please let me know in comments.

这里是如何从文件填充字典.

Here is how you can populate your dictionary from a file.

'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

这篇关于如何建立这个列表并将其随机化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆