如何从字符串列表中输出随机字符串不重复VB.Net

时间:2014-05-19 07:46:57

标签: vb.net

我为我的第一次任务做了多项选择测验11 我的随机数发生器多次输出一些问题,这些问题很容易修复,但我无法弄明白。

以下是我的声明

 Dim questions As New List(Of String)
 Dim answers As New List(Of String)
 Dim check As New List(Of String)
 Dim gotQA As Boolean = False
 Dim currentQuestion As Integer = -1
 Dim randomQuestions As Integer = 0
 Dim random As Random = New Random()
 Dim rightQuestions As New List(Of String)
 Dim wrongQuestions As New List(Of String)
 Public Shared Carrytof3 As String

这是下一个问题按钮代码(验证器)

Private Sub CorrectAns()
    Dim ans As String = TextBox1.Text
    check.Add(Label1.Text)
    If (ans = answers(currentQuestion)) Then
        rightQuestions.Add(currentQuestion)
        If rightQuestions.Count() = 0 Then
            Dim percent As Integer = (rightQuestions.Count() / 20) * 100
            Carrytof3 = (Form1.fname & " " & Form1.lname & " " & "That's Terrible " & "You scored " & rightQuestions.Count() & " Out of 20" & vbNewLine & "Thats " & percent & "%")
        End If
        If rightQuestions.Count() >= 0 Then
            If rightQuestions.Count() <= 4 Then
                Dim percent As Integer = (rightQuestions.Count() / 20) * 100
                Carrytof3 = (Form1.fname & " " & Form1.lname & " " & "BAD LUCK!, Try Again " & "You scored " & rightQuestions.Count() & " Out of 20" & vbNewLine & "Thats " & percent & "%")
            End If
        End If
        If rightQuestions.Count() >= 5 Then
            If rightQuestions.Count() <= 10 Then
                Dim percent As Integer = (rightQuestions.Count() / 20) * 100
                Carrytof3 = (Form1.fname & " " & Form1.lname & " " & "Good Effort You can do better " & "You scored " & rightQuestions.Count() & " Out of 20" & vbNewLine & "Thats " & percent & "%")
            End If
        End If
        If rightQuestions.Count() >= 11 Then
            If rightQuestions.Count() <= 19 Then
                Dim percent As Integer = (rightQuestions.Count() / 20) * 100
                Carrytof3 = (Form1.fname & " " & Form1.lname & " " & "Well Done Nearly There " & "You scored " & rightQuestions.Count() & " Out of 20" & vbNewLine & "Thats " & percent & "%")
            End If
        End If
        If rightQuestions.Count() = 20 Then
            Dim percent As Integer = (rightQuestions.Count() / 20) * 100
            Carrytof3 = (Form1.fname & " " & Form1.lname & " " & "Excellent Job " & "You scored " & rightQuestions.Count() & " Out of 20" & vbNewLine & "Thats " & percent & "%")
        End If
    Else : wrongQuestions.Add(currentQuestion)
    End If
    TextBox1.Text = ""
    If (Not randomQuestions >= 20) Then
        Dim tempR As Integer = currentQuestion

        Do Until Not tempR = currentQuestion
            tempR = random.Next(questions.Count())
        Loop
        currentQuestion = tempR
        lblQuest.Text = questions(currentQuestion)
        randomQuestions += 1

    Else

        Form3.Show()
        Me.Hide()
        MsgBox("Total Right: " & rightQuestions.Count() & vbNewLine & "Total Wrong: " & wrongQuestions.Count())

        Dim wrongQs As String = ""
        For Each wrong As String In wrongQuestions
            If (wrongQs.Count() > 0) Then
                wrongQs &= ", " & questions(wrong)

            Else
                wrongQs &= questions(wrong)

            End If

        Next

        If (wrongQs.Count() > 0) Then MsgBox("Wrong Questions: " & vbNewLine & wrongQs)
    End If


End Sub

这里有加载问题并添加到字符串代码列表

Private Sub questionGet()
    If (randomQuestions > 0) Then randomQuestions = 0
    Using sr As New StreamReader("Questions.txt")
        Using sr2 As New StreamReader("Answers.txt")

            While sr.Peek <> -1
                While sr2.Peek <> -1
                    Dim line As String = sr.ReadLine()
                    Dim line2 As String = sr2.ReadLine()
                    If (line.Contains("|")) Then
                        Dim splits As String() = line.Split("|")
                        Dim splits1 As String() = line2.Split("|")
                        If (Not gotQA) Then gotQA = True
                        questions.Add(splits(0) & vbNewLine & splits(1) & vbNewLine & splits(2) & vbNewLine & splits(3) & vbNewLine & splits(4) & vbNewLine)
                        answers.Add(splits1(0))
                    End If
                End While
            End While
        End Using
    End Using

End Sub

1 个答案:

答案 0 :(得分:1)

有两种选择:

  1. 从问题列表中删除当前问题,以便您只有未提出问题的列表。
  2. 维护另一个布尔值数组或字典,其中键为问题,值为布尔值,表示是否已询问此问题。
  3. 在第一个选项中,由于只存在未提出的问题,因此随机将永远不会重复该问题。 在第二个选项中,如果随机引用已经问过的问题,则另外随机获取。重复,直到你得到一个未回答的问题。