在vb2010中平均取10个样本

时间:2014-09-25 08:51:59

标签: vb.net

我使用下面的代码来查找10个样本的平均值。但是在第一次采样并进行平均时。在下一个周期中,计数器不会变为零。文本框不会更新

Static counter As Integer = 0
DIm average_sum As Double = 0

If counter < 10 Then
    counter = counter + 1
    Count_val.Text = counter
    Dim array(10) As Double
    For value As Integer = 0 To counter
         array(counter) = k
         average_sum = average_sum + array(counter)
    Next
    If counter = 10 Then
        average_sum = average_sum / array.Count
        System.Threading.Thread.Sleep(250)
        Array_count.Text = average_sum

    End If
    If counter > 10 Then
        average_sum = 0
        counter = 0

    End If


End If

1 个答案:

答案 0 :(得分:1)

   If Avg_count < 10 Then
                Dim array(10) As Double
                For value As Double = 0 To Avg_count
                    array(Avg_count) = k
                    average_sum = average_sum + array(Avg_count)
                    Avg_count = Avg_count + 1
                Next

                If Avg_count = 10 Then
                    average_sum = average_sum / Avg_count
                    System.Threading.Thread.Sleep(250)
                    Average.Text = average_sum
                    Avg_count = 0
                End If

            End If

此处正确计算值设置。但是在2到3个周期之后,平均值会更早地完成,我在Excel中写的比较平均值但不匹配平均值和Excel表格数据

下面是excel表格代码。两个代码都在timer1块中。

If counter < 10 Then
                'counter = 0
                'average_sum = 0


                Dim headerText = ""
                Dim csvFile As String = IO.Path.Combine(My.Application.Info.DirectoryPath, "Current.csv")
                If Not IO.File.Exists((csvFile)) Then
                    headerText = "Date,TIME ,Current, "

                End If

                Using outFile = My.Computer.FileSystem.OpenTextFileWriter(csvFile, True)
                    If headerText.Length > 0 Then
                        outFile.WriteLine(headerText)
                    End If
Dim date1 As String = "25-10-2014"
                    Dim time1 As String = TimeOfDay()
                    Dim x As String = date1 + "," + time1 + "," + distance
                    outFile.Write(x)

                End Using
            End If
            If counter > 10 Then
                counter = 0
            End If