简体   繁体   English

在visual basic中为数组赋值

[英]Assigning a value to array in visual basic

This visual basic program prompts the user to interactively enter 4 integer values, which the program stores in an array.这个可视化的基本程序提示用户交互输入 4 个整数值,程序将这些值存储在一个数组中。 It should then find the minimum and maximum values stored in the array, as well as the average of the 4 values.然后它应该找到存储在数组中的最小值和最大值,以及 4 个值的平均值。 The code is代码是

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

I ran the code but it throws this indexoutofbound exception我运行了代码,但它抛出了这个 indexoutofbound 异常

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

I am not sure how to fix this code.我不确定如何修复此代码。 I also think that my code( Averages(LoopIndex) += BattingAverage ) to assign a value to array is not right.我还认为我的代码( Averages(LoopIndex) += BattingAverage )为数组赋值是不正确的。 please help请帮忙

There are a couple things wrong here.这里有一些错误。 First off if you want to take 4 values, you need to change MAX_AVERAGES = 4. The error is coming from this line首先,如果你想取 4 个值,你需要改变 MAX_AVERAGES = 4。错误来自这一行

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

because here LoopIndex has been incremented to 3, which is out of bounds of an array of size 3. Averages(2) is the last index.因为这里 LoopIndex 已经增加到 3,超出了大小为 3 的数组的边界。Averages(2) 是最后一个索引。 You should change the line to您应该将行更改为

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

obl is correct on the above part with array size and was close on the print portion. obl 在数组大小的上述部分上是正确的,并且在打印部分上很接近。

Your Original :您的原件:

                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

How it should read:应该怎么读:

         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

The code was not able to execute based on how large your array was as your code reads that it should print all values in the array, if your array was only one value in size, it would have been able to print.代码无法根据数组的大小执行,因为您的代码读取它应该打印数组中的所有值,如果您的数组大小只有一个值,则它本来可以打印。 So, you need a For LoopIndex = "bounds of the array" for this to work.所以,你需要一个 For LoopIndex = "bounds of the array" 才能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM