txt文件数组中的Sum和Average VBA

时间:2017-11-09 15:56:41

标签: arrays vb.net visual-studio

我试图在列表中显示包含餐馆名称和净值的txt文件的平均值。我在标签中显示它。我还在学习Visual Basic,所以我甚至不确定我的代码是不是一堆垃圾。这就是我所拥有的。它不仅没有显示任何内容,而且还有错误

  

"结构"整数"无法编入索引,因为它没有默认值   值"

代表intNumber。这就是我所拥有的:

Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles 
btnCompute.Click
    Dim objReader As IO.StreamReader
    Dim strLocationAndNameOfFile As String = "I:\franchise.txt"
    Dim IntTotal As Integer
    Dim intNumber As Integer
    Dim intElement As Integer
    Dim intAverage As Integer
    Dim intCount As Integer = 10
    If IO.File.Exists(strLocationAndNameOfFile) Then
        objReader = IO.File.OpenText(strLocationAndNameOfFile)
        For Each intElement In intNumber(strLocationAndNameOfFile)
            IntTotal += intElement
        Next

        intAverage = IntTotal / intCount
        lblAverageCost.Text = intAverage.ToString("C")
    End If

End Sub

1 个答案:

答案 0 :(得分:0)

虽然您正在打开文本文件,但您实际上并没有阅读它。我还有一些略微不同的做法 - 看看代码和评论。

    Dim strLocationAndNameOfFile As String = "I:\franchise.txt"
    Dim IntTotal As Integer
    Dim intAverage As Integer
    Dim intNumberCount As Integer
    'Use a string array to store the file contents

    'instead of using a separate intElements variable (which btw you defined as an individual
    'integer instead of an array, just use the string array to store the text of the file
    'without also having to use an integer array. In the loop below, each element of the string
    'array is parsed into an integer as needed, so no need for an extra array
    Dim fileLines() As String

    If IO.File.Exists(strLocationAndNameOfFile) Then
        'The  using statement opens the file and the end using statement will close the file and
        'dispose of the sr object. Its important to close a file when you're done using it
        Using sr As New StreamReader(strLocationAndNameOfFile)
            'This line does three things. .ReadToEnd reads all the lines in the file and then splits the
            'lines into individual strings. I'm assuming that the lines end with a VbCrLf and not a VbCr
            'Finally those individual lines are stored in the string array
            fileLines = sr.ReadToEnd.Split(vbCrLf)
            'file is closed here and the sr object is disposed
        End Using

        'Here the code loops through each element of fileLines and trys to parse them into an integer and
        'assin the integer to intNumber. If successful, the number is added to the total and 1 is added to
        'intNumberCount to keep track of the number of successfully parsed numbers
        For Each line As String In fileLines
            Dim intNumber As Integer
            If Integer.TryParse(line, intNumber) Then
                IntTotal += intNumber
                intNumberCount += 1
            End If
        Next
        'finally the average is calculated 
        intAverage = IntTotal / intNumberCount
        lblAverageCost.Text = intAverage.ToString("C")
    End If