【问题标题】:Visual Basic: Index was outside bounds of arrayVisual Basic:索引超出数组范围
【发布时间】:2012-10-09 03:38:51
【问题描述】:

我试图让这个 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

【问题讨论】:

    标签: arrays vb.net visual-studio-2010


    【解决方案1】:

    您将players()roll=0 联系起来,然后循环直到roll=5。这显然超出了您的数组范围。

    【讨论】:

    • 糟糕,这是一个非常明显的错误,谢谢。 (将在 7 分钟内给出最佳答案)。
    【解决方案2】:

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

    Dim MaxRolls as integer
    MaxRolls = 5
    

    然后:

    Dim players(player,MaxRolls) As Integer 
    

    应该可以解决的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多