2013-03-08 12 views
0

高级VB班的大学生转向论坛寻求帮助 - 我找到了一些代码示例,但是很难找出这个代码..任何和所有的帮助表示赞赏:)VB.net中.txt文件数组的求和元素

此应用程序导入一个.txt文件存储在bin中,调试文件夹名为data.txt ..20记录,每条记录3行,最后一行是学生的成绩,我需要通过将每个记录等级相加并除以20来平均等级,然后在显示平均值的第一个盒子上显示。

到目前为止,我已经得到了..

Dim objReader As IO.StreamReader 
    Dim intFill As Integer 
    Dim intCount As Integer = 0 
    Dim intAverage As Integer 
    Dim strLocationAndNameOfFile As String = "data.text" 

    If IO.File.Exists(strLocationAndNameOfFile) = True Then 
     objReader = IO.File.OpenText(strLocationAndNameOfFile) 
    Else 
     MsgBox("The file is not available. Restart the program when the file is avilable", , "Error") 
     Me.Close() 
    End If 

    If IO.File.Exists(strLocationAndNameOfFile) Then 
     objReader = IO.File.OpenText(strLocationAndNameOfFile) 
     Do While objReader.Peek <> -1 
      _strName(intCount) = Convert.ToString(objReader.ReadLine()) 
      _strItemID(intCount) = Convert.ToString(objReader.ReadLine()) 
      _intGrade(intCount) = Convert.ToInt32(objReader.ReadLine()) 
      intCount += 1 
     Loop 
     objReader.Close() 
    End If 

    For intFill = 0 To (_strName.Length - 1) 
     *'intAverage = SUM OF ALL AVERAGES/LENGTH OF ARRAY -1* 
     Me.lstAverage.Items.Add(intAverage.ToString()) 
+0

我米不知道我可以看到'Dim strLocationAndNameOfFile As String =“data.text”'working .. – christopher 2013-03-08 21:31:12

+0

它看起来像你的大学教你使用类型前缀。你应该和那些教授聊聊......这曾经很重要,但不再推荐**。请参阅Microsoft自己的编码风格指南,尤其是[本节](http://msdn.microsoft.com/zh-cn/library/ms229045.aspx)(建议不要使用粗体字)“不要使用匈牙利语符号” 。 – 2013-03-08 21:50:44

+0

此外,避免'File.Exists()'http://stackoverflow.com/q/673654/3043,因为这是一个高级的类,你应该知道关闭你的'objReader'对象在Finally块(可能通过一个使用块)。 – 2013-03-08 21:58:01

回答

1

虽然循环读取成绩总结起来

Dim total as Integer 
    Do While objReader.Peek <> -1 
     _strName(intCount) = Convert.ToString(objReader.ReadLine()) 
     _strItemID(intCount) = Convert.ToString(objReader.ReadLine()) 
     _intGrade(intCount) = Convert.ToInt32(objReader.ReadLine()) 
     total += _intGrade(intCount) 
     intCount += 1 
    Loop 

然后就是除以20或_intGrade.Length

intAverage = total/_intGrade.Length 
1

这么多问题,我讨厌做别人的功课,我想让你看看这可能看起来像什么

Public Function GetAverageGrade(ByVal filename As String) As Double 
    Dim totalGrade As Integer = 0 
    Dim lineCount As Integer = 0 
    Dim line As String 

    Using rdr As New IO.StreamReader(filename) 
     While (line = rdr.ReadLine()) IsNot Nothing 
      lineCount += 1 
      If lineCount Mod 3 = 0 Then totalGrade += Convert.ToInt32(line) 
     End While 
    End Using 
    Return totalGrade/(lineCount/3.0) 
End Function 

当然,您可能想要使用该数据做的更多,而不仅仅是获得平均成绩。因此,一个更好的选择是建立代码读取这一切在为一组记录:

Public Class GradeItem 
    Public Property Name As String 
    Public Property Item As String 
    Public Property Grade As Integer 
End Class 

然后

Public Iterator Function ReadGradeItems(ByVal fileName As String) As IEnumerable(Of GradeItem) 
    Using rdr As New IO.StreamReader(fileName) 
     While rdr.Peek() <> -1 
      Yield New GradeItem With {.Name = rdr.ReadLine(), .Item= rdr.ReadLine(), .Grade = Convert.ToInt32(rdr.ReadLine()} 
     End While 
    End Using 
End Function 

,现在把它放在一起:

Dim grades As IEnumerable(Of GradeItem) = ReadGradeItems("data.text") 

lstAverage.Items.Add(grades.Select(Function(g) g.Grade).Average())