gpt4 book ai didi

vb.net - 如何检查测验答案是否正确?

转载 作者:行者123 更新时间:2023-12-02 03:37:54 25 4
gpt4 key购买 nike

我正在为我的计算机科学课做一个测验,基本概念是你有 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 上我需要检查答案是否成功。换句话说,检查所检查的定义是否是关键字的正确答案。如果是,则显示正确答案消息,否则显示错误消息并再次随机化。

另一个要求是每个关键字应该与其定义匹配两次,完成后将从列表中删除并且不再显示。换句话说,每个问题必须完成两次,完成后该问题将不会再次显示。当所有问题都回答两次后,测验结束。我不知道该怎么做,所以非常感谢您的帮助。

抱歉,如果很难理解我在问什么,我会尽量说清楚。

最佳答案

部分问题是字典结构只包含两部分;在这种情况下,问题和答案。然而,还需要第三部分:回答的次数。这可以通过创建一个 Answer 类来实现,该类包含答案字符串和一个 Answered bool 值,例如:

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

kvkeywordcorrectDefinition 必须是表单的全局变量,以允许与 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_loadbutton1_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

关于vb.net - 如何检查测验答案是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22159562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com