0

我已经随机化了一个数组,因此每次播放测验时都会以不同的顺序显示问题,但如果问题无法重复,我会更喜欢它。

这是我的数组的设置方式:

Function loadQuestions()

    Questions(0).Question = "Which of these words are an adjective?"
    Questions(0).option1 = "Dog"
    Questions(0).option2 = "Beautiful"
    Questions(0).option3 = "Steven"
    Questions(0).option4 = "Bird"
    Questions(0).Answer = "B"

    Questions(1).Question = "What's the adjective in this sentence:" & vbCrLf & "'Kelly handled the breakable glasses very carefully'"
    Questions(1).option1 = "Kelly"
    Questions(1).option2 = "Handled"
    Questions(1).option3 = "Carefully"
    Questions(1).option4 = "Breakable"
    Questions(1).Answer = "D"
    ...

这是在测验开始时调用问题的函数。

Function GetQuestion(ByVal intQuestion As Integer)

    tmrOne.Start()

    If questionNumber < 11 Then
        lblQuestionNumber.Text = "Question" & " " & questionNumber
        Dim questionChosen As Integer
        questionChosen = random.Next(25)
        lblQuestion.Text = Questions(questionChosen).Question
        btnAnswerA.Text = Questions(questionChosen).option1
        btnAnswerB.Text = Questions(questionChosen).option2
        btnAnswerC.Text = Questions(questionChosen).option3
        btnAnswerD.Text = Questions(questionChosen).option4
        strAnswer = Questions(questionChosen).Answer

        questionNumber = questionNumber + 1
        btnAnswerA.BackColor = Color.White
        btnAnswerB.BackColor = Color.White
        btnAnswerC.BackColor = Color.White
        btnAnswerD.BackColor = Color.White
        btnAnswerA.Enabled = True
        btnAnswerB.Enabled = True
        btnAnswerC.Enabled = True
        btnAnswerD.Enabled = True
        Return intQuestion
    Else
        MsgBox("You have finished")
        End
    End If

End Function

我试图在互联网上找到一些东西来帮助解决这个问题,但由于我是新手,所以没有成功或理解它。我找到了 ArrayList.RemoveAt 但不确定这是与我的数组一起使用的正确语法吗?

那么,如何阻止我的数组重复一个已经被问过的问题呢?我是否将它们放入另一个数组中?

任何帮助是极大的赞赏!

4

1 回答 1

1

正如我从您的问题中了解到的那样,您使用的是ArrayList. 在这种情况下,是的,这个RemoveAt选项听起来是最好的选择。请记住,纯数组(例如Dim Questions() As String)是最有效的Collection; Lists/的主要优点ArrayLists正是添加/删除元素的容易程度,因此如果您使用 ArrayList,则可以更好地最大化其定义功能之一。

在您的特定情况下的额外优势是,每次您删除一个项目时,它的位置都会被填充(并且项目总数减少)。因此,您只需将随机数生成器的最大值更新为当前索引数(不是元素,因为第一个索引为零)。

总之,您的代码中的以下更改将提供您想要的内容:

If(Questions.Count > 0) Then
    questionChosen = random.Next(0, Questions.Count - 1)
End If

一旦你完成了它:

If(questionChosen >= 0 And questionChosen <= Questions.Count - 1) Then
    Questions.RemoveAt(questionChosen)
End If
于 2013-08-25T09:42:05.807 回答