2009年5月19日 星期二

多行資料求平均及最大最小值

image

image

'多行資料求平均及最大最小值
Public Class Form1
    Dim i, s
    Dim c(60) As Integer
    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("C:\d1.txt")
        '取代分行符號為空白間隔
        fileContents = Replace(fileContents, vbNewLine, " ")
        '去檔案後面空白
        fileContents = Trim(fileContents)
        Dim c1() = Split(fileContents, " ")
        For i = 0 To UBound(c1)
            c(i) = Val(c1(i))
        Next

        Dim cMax = c(0)
        Dim cMin = c(0)
        For i = 0 To UBound(c1)
            If c(i) > cMax Then cMax = c(i)
            If c(i) < cMin Then cMin = c(i)
            s = s + c(i)
        Next
        Dim str1 = "最大:" & cMax & vbNewLine & "最小:" & cMin & vbNewLine & "平均:" & _
                    Int(s / (UBound(c1) + 1) * 10 + 0.5) / 10 & vbNewLine & "共" & UBound(c1) + 1 & "筆"
        MsgBox(str1)
        ' My.Computer.FileSystem.WriteAllText("C:\d2.txt", str1, True)
        End
    End Sub
End Class

沒有留言:

張貼留言