Arrays 如何阻止数组在Visual Basic 2010中重复?

Arrays 如何阻止数组在Visual Basic 2010中重复?,arrays,vb.net,random,Arrays,Vb.net,Random,我随机化了一个数组,所以每次测验时问题都会以不同的顺序显示,但如果问题不能重复,我更愿意这样做 以下是我的阵列的设置方式: Function loadQuestions() Questions(0).Question = "Which of these words are an adjective?" Questions(0).option1 = "Dog" Questions(0).option2 = "Beautiful" Questions(0).opti

我随机化了一个数组,所以每次测验时问题都会以不同的顺序显示,但如果问题不能重复,我更愿意这样做

以下是我的阵列的设置方式:

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
函数GetQuestion(ByVal intQuestion作为整数) tmrOne.Start() 如果问题编号<11,则 lblQuestionNumber.Text=“问题”&”“&questionNumber 被选为整数的整数 问题选择=随机。下一步(25) lblQuestion.Text=问题(已选择的问题)。问题 btnAnswerA.Text=问题(已选择的问题)。选项1 btnAnswerB.Text=问题(选择的问题)。选项2 btnAnswerC.Text=问题(已选择的问题)。选项3 btnAnswerD.Text=问题(已选择的问题)。选项4 strAnswer=问题(已选择的问题)。回答 问题编号=问题编号+1 btnAnswerA.BackColor=Color.White btnAnswerB.BackColor=颜色.白色 btnAnswerC.BackColor=颜色.白色 btnAnswerD.BackColor=颜色.白色 btnAnswerA.Enabled=True btnAnswerB.Enabled=True btnAnswerC.Enabled=True btnAnswerD.Enabled=True 返回问题 其他的 MsgBox(“您已完成”) 终点 如果结束 端函数 我曾试图在互联网上找到一些东西来帮助解决这个问题,但没有成功,也没有理解它,因为我是新手。我找到了ArrayList.RemoveAt,但不确定这是否是用于我的数组的正确语法

那么,一旦我的数组被询问,我如何阻止它重复一个问题呢?我要把它们放到另一个数组中吗


非常感谢您的帮助

我从你的问题中了解到,你使用的是
ArrayList
。在这种情况下,是的,
RemoveAt
选项听起来是最好的选择。请记住,纯数组(例如,
Dim Questions()作为字符串
)是最有效的
集合类型
列表
/
数组列表
的主要优点就是添加/删除元素非常简单,因此如果使用数组列表,可以更好地最大化其定义功能之一

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

总之,代码中的以下更改将满足您的需要:

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

如果(questionselected>=0和questionselected您已经问了这个问题。同样,Hans Passant是对的,您不应该一遍又一遍地发布相同的问题;您应该等到上一个问题得到正确的答案(通过进一步编辑问题并自己进行工作/研究)。无论如何,我不完全同意这是所提供链接的副本;因为它处理数组(更复杂)。使用列表/数组列表,事情变得容易多了(如我的答案所示)。问题甚至与最棘手的问题(避免重复元素)不一样:通过列表+删除,您可以轻松摆脱它。