简体   繁体   中英

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. It should then find the minimum and maximum values stored in the array, as well as the average of the 4 values. 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

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. 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

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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