0

到目前为止,我已经在 Virtual Basic 2010 中创建了一个随机发生器,它:

  1. 将用户在 form2 中输入的人名放入一个数组(以空格分隔)。
  2. 随机化数组
  3. 为每个估算的名称显示带有连续数字和句点的数组。

这是源代码:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim names() As String
    Dim i As Integer
    Dim j As Integer
    Dim tmp As String
    Dim txt As String

    ' Put the names in an array. SaveTitle is all the text saved in Form2
    names = Split(My.Settings.SaveTitle, " ")

    ' Randomize the array.
    Randomize()
    For i = LBound(names) To UBound(names) - 1
        ' Pick a random entry.
        j = Int((UBound(names) - i + 1) * Rnd() + i)

        ' Swap the names.
        tmp = names(i)
        names(i) = names(j)
        names(j) = tmp
    Next i

    ' Display the results.
    For i = LBound(names) To UBound(names)
        txt = txt & vbCrLf & i + 1 & ". " & names(i)
    Next i
    txt = Mid$(txt, Len(vbCrLf) + 1)

    RichTextBox1.Text = txt
End Sub

注意最后一点。我想获取变量 txt 并将其拆分。然后我想取前 10 个名字并在 RichTextBox1 中显示,接下来的 10 个名字在 RichTextBox2 中显示,最后 10 个名字在 RichTextBox3 中显示。

我怎样才能做到这一点?

4

1 回答 1

1

试试这个方法。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim lstNames() As String
        Dim txt1, txt2, txt3 As String

        ' Put the names in an array. SaveTitle is all the text saved in Form2
        lstNames = Split(My.Settings.SaveTitle, " ")

        ' Randomize the array.
        Randomize()

        ' try this 
        For n As Integer = 0 To lstNames.Count - 1
            lstNames(n) = String.Format("{0}{1} ", lstNames(n), Rnd() + n)
        Next

        For n As Integer = 0 To lstNames.Count - 1
            If n < 10 Then
                txt1 += lstNames(n)
            End If

            If n > 9 And n < 20 Then
                txt2 += lstNames(n)
            End If

            If n > 19 And n < 30 Then
                txt2 += lstNames(n)
            End If

        Next
        RichTextBox1.Text = txt1
        RichTextBox2.Text = txt2
        RichTextBox3.Text = txt3

    End Sub
于 2012-04-24T19:36:28.980 回答