我只是将所有的代码放在这里,以防在“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个输出给出。
你们有没有机会帮我找出问题所在?
从我到目前为止所了解到的,它正在执行正确数量的循环等等。只要它开始为第二个游戏键生成“赢家”,它就不会从字符串中获取字符串值namesarray。
另外,为什么是
For x = 0 To totalnames - 1
Debug.Print("namesarray(" & x & ") = " & namesarray(x))
Next
不给我一个调试输出?
(有点偏离主题)是否有一个特定的原因,你为什么要在一个由逗号分隔的字符串中存储一个名称集合?难道它不会简化事物来创建'Dim Names As New List(Of String)'? - 我正在审视这个实际的问题,但我不得不提到这一点。 – hometoast
嗯,我还在学习,这就是为什么我总是很欣赏那些超越问题寻找的人帮助我改进我的代码:)所以,感谢您指出了这一点^^ – Yorrick
PS:离题我的屁股,用一个替换阵列列表(的字符串)修复了所有xD谢谢兄弟:) 好吧,除了Debug.print – Yorrick