VB.NET - 累积数字和计算平均值

时间:2013-10-07 00:28:41

标签: vb.net accumulate

我已经重新开始了,除了2个累积总计之外,一切都按预期工作,计算不按我需要的方式工作。而不是将以前的销售额添加到总数中,而是在最后处理新的销售......

例如:我输入了1个Snowboard和1个Snowboard with Boots,这个有效,1个显示在Snowboards和Snowboards with Boots的摘要中。然而,当我尝试再次输入以保持销售总额时我遇到问题,我输入1个Snowboard和1个Snowboard with Boots,摘要显示11而不是2.为什么会发生这种情况?

   ' Declare module-level variables and constants.
Private SnowBoardsSold, BootsSold, SaleCount As Integer
Private ItemsSold As Integer
Private TotalSales As Decimal
Const SNOWBOARD_RATE As Decimal = 20D
Const BOOTS_RATE As Decimal = 30D


Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
    ' Calculate the prices
    Dim SnowBoards, Boots As Integer
    Dim SnowBoardSale, BootsSale, ThisSale, AverageSalesEver As Decimal

    Try
        ' Convert the Snowboard input to numeric variable.
        SnowBoards = Integer.Parse(SnowboardsTextBox.Text)

        Try
            ' Convert Boots if Snowboard was successful.
            Boots = Integer.Parse(WithBootsTextBox.Text)
            ' Calculate values for sale.
            SnowBoardSale = SnowBoards * SNOWBOARD_RATE
            BootsSale = Boots * BOOTS_RATE
            ThisSale = SnowBoardSale + BootsSale

            ' Calculate summary values.
            TotalSales += ThisSale
            BootsSold += BootsSale
            SnowBoardsSold += SnowBoardSale
            SaleCount += 1
            AverageSalesEver = TotalSales / SaleCount

            ' Format and display prices for the sale.
            SnowBoardsPriceTextBox.Text = SnowBoardSale.ToString("c")
            BootsPriceTextBox.Text = BootsSale.ToString("c")
            TotalPriceTextBox.Text = ThisSale.ToString("c")

            ' Format and display values for the summary.
            SnowBoardRentalTextBox.Text += SnowBoards.ToString()
            BootsRentalTextBox.Text += Boots.ToString()
            TotalChargesTextBox.Text = TotalSales.ToString("c")
            AverageChargeTextBox.Text = AverageSalesEver.ToString("c")

        Catch BootsException As FormatException
            ' Handle a Boots exception.
            MessageBox.Show("Please enter numbers.", "Data Entry Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error)
            With WithBootsTextBox
                .Focus()
                .SelectAll()
            End With
        End Try

    Catch SnowBoardsException As FormatException
        ' Handle a SnowBoard exception.
        MessageBox.Show("Please enter numbers.", "Data Entry Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Error)
        With SnowboardsTextBox
            .Focus()
            .SelectAll()
        End With

    Catch AnException As Exception
        'Handle any other exception.
        MessageBox.Show("Error: " & AnException.Message)
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

首先,您可能想要更改获取数字的方式:

'Convert snowboard input value to numeric variable.
SnowboardInteger = Integer.Parse(SnowboardsTextBox.Text)

为:

Dim SnowboardInteger As Integer
If TryParse.Integer(SnowboardsTextBox.Text, SnowboardInteger) = False then
 ' if it parses, SnowboardInteger will have the value, 
 'else the function returns false
     MessageBox.Show("Please enter numbers")
End if

此外,您正在解析文本框两次。一旦进入声明,然后立即。这看起来不对:

SnowboardSaleCountInteger += 1
WithBootsSaleCountInteger += 1
TotalSaleCountInteger += TotalChargesSumInteger   ' ?????
AverageChargeInteger = TotalChargesSumInteger / TotalSaleCountInteger

向“柜台”添加“费用”似乎不对。如果你想要avg不应该是:

TotalSaleCountInteger = SnowboardSaleCountInteger + WithBootsSaleCountInteger 

如果你想在结果中得到像'xx.yy'这样的分数,AverageChargeInteger应该是一个Double或Decimal,除非整个美元可以用于分配。

如果您需要累积之前的点击次数,那么您需要将某些声明从SnowboardSumInteger, WithBootsSumInteger移出程序,以便它们可以累积。实际上,WithBootsSaleCountInteger, TotalSaleCountInteger没有做任何事情,总是1.我想知道作为'SaleCounter'他们不应该增加文本框中的值而不是1(没有在我面前的任务)。< / p>

您可能需要做的另一件事是:

SnowboardPriceInteger = SnowboardInteger * SNOWBOARD_RENTAL_RATE
WithBootsPriceInteger = WithBootsInteger * WITH_BOOTS_RENTAL_RATE
TotalPriceInteger = SnowboardPriceInteger + WithBootsPriceInteger

你的常量是十进制(SNOWBOARD_RENTAL_RATE)所以,计算的结果必须进入十进制变量,但是SnowboardPriceInteger而另一个则不是。' sales accumulators Private TotalSales As Decimal = 0 Private ItemsSold As Integer = 0 。整数不能存储乘法或除法的小数结果。

类似的东西:

' this sale:
Dim Snowboards As Integer 
Dim Boots As Integer

' get the values from the text boxes, then:
Dim SnowBoardSale As Decimal = (SnowBoards * SnowBoardRate)
Dim BootsSale As Decimal = (Boots * BootsRate)
Dim ThisSale As Decimal =  SnowBoardSale  + BootsSale 

 ' update accumulators
TotalSales += ThisSale
ItemsSold += (Boots + Snowboards)

' calc the average
Dim AverageSalesEver AS Decimal = TotalSales  / ItemsSold 

' Display results in textboxes
'(this exercise is left to the student  ;)  )

点击活动:

{{1}}

HTH

我认为你只是把自己与过多的变数混淆了,而旧的尝试遗留了这些变量。