0

我只是将我所有的代码都扔在这里,以防“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

不给我调试输出?

4

2 回答 2

1

简化您的问题。

将 names 设为 List(Of String),而不是在字符串中添加“,name”,而是使用names.Add(namereadfromconsole). 一个简单的 names.Contains(thename) 可以替换inArray您正在使用的标志,而不是遍历名称字符串。而不是movenames()调用,而是一个简单的names.Remove(nametoremove).

至于 Debug.Print() 调用不显示任何内容,请尝试在 Options->Debugging->General->[x] Redirect all Output Window text to the Immediate Window 下进行检查。

于 2012-06-14T12:00:16.403 回答
1

主要错误在这里

    For x = index To totalnames - 1
        namesarray(index) = namesarray(index + 1)
    Next

我认为你应该这样做

    For x = index To totalnames - 1
        namesarray(x) = namesarray(x + 1)
    Next

请记住,如果随机索引等于名称数组中的最大值,它将导致您的代码崩溃。(例如:totalnames=6并且index = Int(Rnd() * (totalnames - 1))导致 index=5,然后 movenames 崩溃)

Debug.Print 输出进入即时窗口或输出窗口,而不是代码打开的控制台窗口。为此使用 Console.Writeline。

于 2012-06-14T12:10:59.570 回答