有没有一种简单的方法可以将字符串混洗到数组中?

时间:2021-04-01 17:37:47

标签: vb.net

我在适应 Visual Basic 时遇到了一些麻烦,我认为我的算法现在是错误的。

我必须创建一种单词随机游戏,首先我必须声明一个包含几个单词的数组,然后在同一数组中选择一个随机单词以将其显示在标签中。到目前为止,我成功地做我想做的事。

但问题来了,现在我必须选择同一个词并将其随机混入另一个数组中,以便为用户显示。问题是我第一在visual basic的词汇上苦苦挣扎,第二我认为我的做法是错误的,因为如果这样做会花费很多资源我的“空”条件没有很快被填满。但我不知道除了这个方法还能做什么。

当然在这种状态下它也不起作用,我不确定为什么但充其量我得到一个字符...

这是我目前的代码:

Public Sub shuffle(ByVal chain As String)
        '''METHOD'''
        '''Shuffle the letter randomly'''
        Dim arrayShuffled(theWord.Length) As String
        Dim pointer As Integer = 0
        Dim pointerRand As Integer

        //I want to loop around theWord Array
        For i As Integer = 0 To theWord.Length - 1

            //Try to give a random position arrayShuffle
            pointerRand = (rand.Next(0, arrayShuffled.Length))

            //Loop till you find a null index 
            Do Until arrayShuffled(pointerRand) = ""
                pointerRand = (rand.Next(0, arrayShuffled.Length))
            Loop

            //Then give to this index the char from theWord where index = pointeur
            arrayShuffled(pointerRand) = theWord(pointer)
            pointeur += 1

        Next

        //Here this : (rand.Next(0, ArrayShuffled.Length - 1)) feel wrong but I don't know if we can bypass the condition from the declaration with () in other word I don't really know what to put in the ()
        lblWord.Text = ArrayShuffled(rand.Next(0, ArrayShuffled.Length - 1)) 
    End Sub

我希望我说得够清楚了,谢谢你的帮助。

PS:我很惭愧,但我不确定我现在正在使用的 VB,我认为它是 VB6,但 Visual Studio 没有给我答案

0 个答案:

没有答案
相关问题