我只是将我所有的代码都扔在这里,以防“SelectName()”子中的一段代码有问题。
Module Module1
Dim selectednames As String = ""
Dim index As Short = 0
Dim inarray As Boolean = False
Dim amountofkeys As Short
Dim namesarray() As String
Dim names As String = ""
Dim input As String = ""
Dim totalnames As Short = 0
Dim indexofcomma As Short = 0
Sub Main()
Console.Write("Howmany keys are there to be given away? ")
amountOfKeys = CShort(Console.ReadLine())
Start()
While Not amountofkeys = -1
SelectName(names, totalnames)
amountofkeys = amountofkeys - 1
End While
Console.Write("The winners are: " & selectednames)
Console.ReadLine()
End Sub
Sub SelectName(ByVal names As String, ByVal totalnames As Short)
ReDim namesarray(totalnames - 1)
If inarray = False Then
For x = 0 To totalnames - 1
indexofcomma = InStr(names, ",")
namesarray(x) = Left(names, indexofcomma - 1)
names = Mid(names, indexofcomma + 1, (Len(names)))
Next
inarray = True
End If
Randomize()
index = Int(Rnd() * (totalnames - 1))
For x = 0 To totalnames - 1
Debug.Print("namesarray(" & x & ") = " & namesarray(x))
Next
selectednames &= namesarray(index) & " "
movenames()
End Sub
Sub movenames()
For x = index To totalnames - 1
namesarray(index) = namesarray(index + 1)
Next
totalnames -= 1
End Sub
Sub Start()
Console.WriteLine("Enter all the viewer's names, one by one.")
Console.WriteLine("Once all names have been entered, press 0.")
input = Console.ReadLine()
While Not input = "0"
names &= input & ","
totalnames += 1
input = Console.ReadLine()
End While
End Sub
End Module
这是它的作用的图像(我想你可以看到出了什么问题)
13 个输入,预期 3 个输出,仅给出 1 个输出。
你们有没有机会帮我找出问题所在?
据我目前所了解的,它正在执行正确数量的循环等。它一旦开始为第二个游戏密钥生成“获胜者”,它就不会从名称数组中获得字符串值。
还有,为什么
For x = 0 To totalnames - 1
Debug.Print("namesarray(" & x & ") = " & namesarray(x))
Next
不给我调试输出?