有条件地从VB中的另外两个数组创建一个数组?

时间:2012-02-20 00:01:55

标签: vb.net arrays

我有两个数组

a={a,b,c,d}
b={1,2,3,4}

现在我想创建一个第三个数组,它至少应包含一个数字和其余的字母,这段代码将生成唯一的用户名,因此它不应重复主数组中存在的选择模式

Darray={abc1, acd2,...}

我联合ab

Dim c As String() = Barray.Union(Aarray).ToArray()

        Dim Carray(0 To 8) As String

我正在考虑在这里做一个while循环,但不知道如何通过这个条件,它应该至少得到一个数字,不应该重复Darray

的模式
do while (confused....)
        Dim rnd As New Random

        For i = 0 To 8

            Carray(i) = c(rnd.Next(0, c.Length))

        Next
loop

2 个答案:

答案 0 :(得分:0)

创建一个表单,并粘贴此鳕鱼。它生成100个代码,也许这就是你想要的?在表单上添加一个列表框,取消注释ADD行以查看结果。

 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim aa() As String = {"a", "b", "c", "d"}
    Dim an() As String = {"1", "2", "3", "4"}
    Dim da As New Collection
    Dim rnd As New Random
    Dim i As Integer = 0
    While i < 100
        Dim j1 As Integer = rnd.Next(4)
        Dim j2 As Integer = rnd.Next(4)
        Dim rs As String = ""
        For k As Integer = 1 To 4
            If k = j2 Then
                rs += an(j1)
            Else
                Dim j3 As Integer = rnd.Next(4)
                rs += aa(j3)
            End If
        Next
        If Not da.Contains(rs) Then
            da.Add(rs, rs)
            ' ListBox1.Items.Add(rs)
            i += 1
        End If
    End While
End Sub

答案 1 :(得分:0)

USER1167053的更新,回答“if i dont want it to repeat”:

用以下内容替换FOR looop:

        Dim k As Integer = 0
        While k < 4
            If k = j2 Then
                rs += an(j1)
                k += 1
            Else
                Dim j3 As Integer = rnd.Next(4)
                If Not rs.Contains(aa(j3)) Then
                    rs += aa(j3)
                    k += 1
                End If
            End If
        End While

请注意解决方案的数量大于您施加的限制,否则程序将进入无限循环