计算平均值的正确方法是什么?

时间:2019-04-30 19:09:28

标签: vb.net average

这是我已经工作了大约一天的项目,我似乎无法像我想要的那样使它正常工作。我知道我需要一个计数器和累加器,但我只是想不出如何在计算平均值时正确使用它。

我尝试通过将累加器(maccum)除以MovingRating来计算平均值,它只会返回看起来像一个无限符号的奇数输出。因此,我尝试将计数器除以MovingRating,但仍然没有运气。

Public Class Form1
    'declaring module level variables
    Dim maccum As Integer
    Dim mCount As Integer
    Dim totalRatings As Short
    Dim MovieRating As Short

    Private Sub btnRateIt_Click(sender As Object, e As EventArgs) Handles btnRateIt.Click
        'Declaring variables
        Dim MovieDescription As String
        Dim MovieRating As Integer
        Dim strFirstSentence As String
        Dim strSecSentence As String
        Dim thankYou As String
        Dim ratingMovie As String
        Dim Stars As String

        'Input
        thankYou = "Thank you for rating this"
        ratingMovie = "movie with"
        Stars = "stars"

        'Process the data
        MovieDescription = UCase(txtMovieDscpt.Text)
        MovieRating = updwnStars.Value
        mCount = mCount + 1
        maccum = maccum + 1
        Label6.Text = maccum
        Label7.Text = MovieRating

        'Ouput
        strFirstSentence = thankYou & " " & MovieDescription
        strSecSentence = ratingMovie & " " & MovieRating & " " & Stars
        lblUserRating.Text = strFirstSentence
        lblRatingDisplay.Text = strSecSentence

    End Sub

    Private Sub btnAvgRating_Click(sender As Object, e As EventArgs) Handles btnAvgRating.Click
        'Declaring variables
        Dim averageRating
        'Process the data
        averageRating = mCount / MovieRating
        'Output the processed data
        lblAvgRating.Text = averageRating.ToString
    End Sub
End Class

我希望将MovieRating的结果除以平均值。我尝试将movierating设置为5,然后将2加到计数器上。因此,当我尝试获得平均值时,它只会产生5/2 = ouput

2 个答案:

答案 0 :(得分:0)

首先,您将MovieRating声明为Short,然后在例程中将其替换为Integer。我的猜测是您的计算使用的是Class Scope变量,而不是Sub范围的值。

另外,请使用Option Strict On

答案 1 :(得分:0)

您的代码中有很多重复的元素,我认为不应(或不需要)。

我认为您对多个声明感到困惑。

看看这段代码,它与您的代码基本相同,但是删除了重复的位

'declaring module level variables
Dim mCount As Integer
Dim totalRatings As Decimal

Private Sub btnRateIt_Click(sender As Object, e As EventArgs) Handles btnRateIt.Click
    'Declaring variables
    Dim thankYou As String = "Thank you for rating this {0}"
    Dim ratingMovie As String = "movie with {0} stars"

    'Process the data
    mCount = mCount + 1
    totalRatings += updwnStars.Value

    Label6.Text = mCount.ToString()
    Label7.Text = updwnStars.Value.ToString()

    'Ouput
    lblUserRating.Text = String.Format(thankYou, UCase(txtMovieDscpt.Text))
    lblRatingDisplay.Text = String.Format(ratingMovie, updwnStars.Value)
End Sub

Private Sub btnAvgRating_Click(sender As Object, e As EventArgs) Handles btnAvgRating.Click
    'Declaring variables
    Dim averageRating As Decimal

    'Process the data
    averageRating = totalRatings / mCount ' Average is calculated by Sum divided by the Count

    'Output the processed data
    lblAvgRating.Text = averageRating.ToString
End Sub