在visual basic中为数组赋值

时间:2017-03-19 21:12:56

标签: arrays vb.net for-loop

此可视化基本程序会提示用户 以交互方式输入4个整数值,即程序 存储在一个数组中。然后它应该找到最小值和最大值 存储在数组中的值,以及4个值的平均值。 代码是

Option Explicit On
        Option Strict On
        Module BattingAverage
       Sub Main()
            Const MAX_AVERAGES AS Integer = 3
            Dim Averages(MAX_AVERAGES -1) as Double
            Dim LoopIndex As Integer
            Dim BattingAverage As Double
            Dim BattingString As String        
            Dim Min As Double
            Dim Max As Double
            Dim Total As Double
            Dim Average As Double

                For LoopIndex = 0 To MAX_AVERAGES - 1
                BattingString = InputBox$("Enter a batting average: ")
                BattingAverage = Convert.ToDouble(BattingString)

              'Assigning a value to Array
                Averages(LoopIndex) += BattingAverage

            Next LoopIndex

            Min = Averages(0)
            Max = Averages(0)
                   Total = Averages(0)
                    For LoopIndex = 1 To Averages.length -1
            If Averages(LoopIndex) < Min then

            Min = Averages(LoopIndex)
            Else If Averages(LoopIndex) > Max then
            Max = Averages(LoopIndex)
            end if
              Total += Averages(LoopIndex)
             ' 
        Next LoopIndex
                Average = Total / MAX_AVERAGES
                System.Console.WriteLine("Batting Averages : " & Averages(LoopIndex))
                System.Console.WriteLine("Maximum value : " &Max)
            System.Console.WriteLine("Minimum value : " &Min)
            System.Console.WriteLine("Average : " &Average)
       End Sub
    End Module

我运行了代码,但它抛出了这个indexoutofbound异常

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at BattingAverage.Main()

我不确定如何修复此代码。我还认为我的代码(Averages(LoopIndex) += BattingAverage)为数组赋值是不对的。请帮忙

2 个答案:

答案 0 :(得分:2)

这里有一些错误。如果您想要获取4个值,首先需要更改MAX_AVERAGES = 4.错误来自此行

System.Console.WriteLine("Batting Averages : " & Averages(LoopIndex))

因为这里LoopIndex已经增加到3,这超出了大小为3的数组的范围。平均值(2)是最后一个索引。您应该将行更改为

Console.WriteLine("Batting Averages: ")
For i = 0 To Averages.Length - 1
    Console.WriteLine(Averages(i).ToString)
Next

答案 1 :(得分:1)

obl在上面具有数组大小的部分上是正确的,并且在打印部分上是紧密的。

您的原件:

                System.Console.WriteLine("Batting Averages : " & Averages(LoopIndex))
                System.Console.WriteLine("Maximum value : " &Max)
            System.Console.WriteLine("Minimum value : " &Min)
            System.Console.WriteLine("Average : " &Average)
       End Sub

应如何阅读:

         For LoopIndex = 0 To 3
           System.Console.WriteLine("Batting Averages : " &Averages(LoopIndex))
         Next
            System.Console.WriteLine("Maximum value : " &Max)
            System.Console.WriteLine("Minimum value : " &Min)
            System.Console.WriteLine("Average : " &Average)

   End Sub

代码无法根据数组的大小执行,因为您的代码读取它应打印数组中的所有值,如果您的数组只有一个值,则可以打印。因此,您需要一个For LoopIndex =“ array of the array”才能起作用。