0

我试图让这个 Visual Basic 程序将值存储在多维数组中。由于索引超出数组范围,我看到的所有其他错误都是由于数组是在变量之前声明的,但我很确定我在正确的位置声明了我的。任何人都可以看到有什么问题吗?

    Randomize()
    Dim roll As Integer = 0
    Dim player As Integer
    Dim index As Integer

        Console.Writeline("Enter the number of players: ")
        index = Convert.ToInt32(Console.Readline)

        player = index - 1


        Dim players(player,roll) As Integer

        Do Until index = 0

            Do Until roll = 5
                players(player,roll) = CInt(Int((6 * Rnd()) + 1))
                roll +=1
            Loop

            player -=1

        Loop
4

2 回答 2

3

你用 标注尺寸players()roll=0然后循环直到roll=5。这显然超出了您的数组范围。

于 2012-10-18T15:58:58.120 回答
1

我会添加一个新变量 - 尝试将您的代码更改为:

Dim MaxRolls as integer
MaxRolls = 5

然后:

Dim players(player,MaxRolls) As Integer 

那应该解决它

于 2012-10-18T16:05:19.207 回答