1

我正在尝试在 VB.net 中创建一个循环算法的函数。我已经使用 Split 命令将算法拆分为一个数组,所以我有一个包含值的数组。然后我尝试遍历它们并在必要时用“数字”替换 #,但是 VB.net 会引发错误。我对 VB.net 还很陌生,所以我不确定它为什么要这样做。算法的格式为A B C D E F 1 2 3 #

Function generate(ByVal alg As String)
    Dim algSplit As String() = alg.Split(" ")
    For Each digit In algSplit
        Dim replacement As String = algSplit(digit).Replace("#", "Number")
        algSplit(digit) = replacement
    Next
    Dim result As String = String.Join("", algSplit)
    MsgBox(result)
End Function

有快速修复吗?

问题第 2 部分:我让循环正常工作;但是我破坏了我想要的部分功能。

Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
    Dim Generator As System.Random = New System.Random()
    Return Generator.Next(Min, Max)
End Function
Public Function RandLet() As String
    Dim number As Integer = GetRandom(1, 26)
    Dim Alphabet() As String = New String() {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V""W", "X", "Y", "Z"}
    Dim Letter As String = Alphabet(number)
    Return Letter
End Function
Function generate(ByVal alg As String) As String
    Dim algSplit As String() = alg.Split(" "c)

    For index As Int32 = 0 To algSplit.Length - 1
        algSplit(index) = algSplit(index).Replace("#"c, GetRandom(1, 9)).Replace("%"c, RandLet())
    Next
    Dim result As String = String.Join("", algSplit)
    MsgBox(result)
    Return result
End Function

最终结果从A B C D E F 1 2 3 | L % % % | N # # #本质ABCDEF123|LXXX|N888 上讲,每个 # 和每个 % 都被相同的数字替换,而不是每次都替换为不同的数字。我以为在循环中不会发生这种情况,我错过了什么?

4

4 回答 4

2

您正在使用字符串变量进行 For-Eaching,因为digit正在查找“A”,它是一个字符串,但您将它用作索引:algSplit(digit).

你可以试试这个:

For i As Integer = 0 To algSplit.Length - 1
  algSplit(i) = algSplit(i).Replace("#", "Number")
Next

如果您只是想替换#和 空格,这也可以:

alg = alg.Replace(" ", String.Empty).Replace("#", "Number")
于 2012-06-01T12:00:08.380 回答
1

使用 LINQ 可能更容易:

Dim value = "A B C D E F 1 2 3 #"
Dim replaced = From digit In value.Split(" "c)
             Select digit.Replace("#"c, "Number")
Dim result = String.Join("", replaced)

您的“传统”方法,更正:

Function generate(ByVal alg As String) As String
    Dim algSplit As String() = alg.Split(" "c)

    For index As Int32 = 0 To algSplit.Length - 1
        algSplit(index) = algSplit(index).Replace("#"c, "Number")
    Next
    Dim result As String = String.Join("", algSplit)
    Return result
End Function
  1. 方法(VB 中的函数)必须返回一些东西。您的函数不返回任何内容,因此AS String在最后添加。
  2. Split有几个重载,您使用的重载需要 aChar而不是字符串,所以替换alg.Split(" ")alg.Split(" "c)
  3. 如果您想用其他值替换数组值,最好 aFor-Loop而不是 a,For-Each因为您需要经常使用索引(见上文)
于 2012-06-01T12:00:32.057 回答
1

您正在循环字符串数组。
algSplit 上的 foreach 返回一个字符串而不是整数。
您不能将该digit字符串用作数组的索引

用这个替换你的代码:

Function generate(ByVal alg As String)     
    Dim algSplit As String() = alg.Split(" ")     
    For digit = 0 to algSplit.Length - 1
        Dim replacement As String = algSplit(digit).Replace("#", "Number")         
        algSplit(digit) = replacement     
               ' could be simply written as 
               ' algSplit(digit) = algSplit(digit).Replace("#", "Number")     

    Next     
    Dim result As String = String.Join("", algSplit)     
    ??? .. you should return something here ????
End Function 
于 2012-06-01T12:00:43.607 回答
-2
Function generate(ByVal alg As String)     
    Dim algSplit As String() = alg.Split(" ")     
    For digit = 0 to algSplit.Length - 1
        Dim replacement As String = algSplit(digit).Replace("#", "Number")         
        algSplit(digit) = replacement     
               ' could be simply written as 
               ' algSplit(digit) = algSplit(digit).Replace("#", "Number")     

    Next     
    Dim result As String = String.Join("", algSplit)     
    ??? .. you should return something here ????
End Function
于 2015-12-04T08:14:31.407 回答