2009年5月18日 星期一

讀入多筆不定長度資料算平均

image

image

'讀入多筆不定長度資料算平均
Public Class Form1
    Dim i, j
    Dim s
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim fileContents As String
        fileContents = My.Computer.FileSystem.ReadAllText("f:\T1.txt")

        Dim r() = Split(fileContents, vbNewLine)
        Dim n = 0
        For j = 0 To UBound(r)
            If r(j) <> "" Then
                Dim c() = Split(r(j), " ")
                For i = 0 To UBound(c)
                    n = n + 1
                    s = s + Val(c(i))
                Next
            End If
        Next
        MsgBox("共" & n & "筆,平均:" & Int(s / n + 0.5))
    End Sub
End Class

沒有留言:

張貼留言