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

VB.NET-累积数字并计算平均值,vb.net,accumulate,Vb.net,Accumulate,我已经重新开始,一切都按预期进行,除了两个累计总数,计算没有按我需要的方式进行。它不是把以前的销售额加到总销售额中,而是在最后增加新的销售额 例如:我输入了1个滑雪板和1个带靴的滑雪板,这样可以工作,1在摘要中显示为滑雪板和带靴的滑雪板。然而,当我再次尝试输入以保持销售总额时,我遇到了一个问题,我输入了1个滑雪板和1个带靴滑雪板,总结显示为11而不是2。为什么会发生这种情况 ' Declare module-level variables and constants. Private Sn

我已经重新开始,一切都按预期进行,除了两个累计总数,计算没有按我需要的方式进行。它不是把以前的销售额加到总销售额中,而是在最后增加新的销售额

例如:我输入了1个滑雪板和1个带靴的滑雪板,这样可以工作,1在摘要中显示为滑雪板和带靴的滑雪板。然而,当我再次尝试输入以保持销售总额时,我遇到了一个问题,我输入了1个滑雪板和1个带靴滑雪板,总结显示为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

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

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

此外,您还要对文本框进行两次解析。一次在申报单上,然后马上。这看起来不对:

SnowboardSaleCountInteger += 1
WithBootsSaleCountInteger += 1
TotalSaleCountInteger += TotalChargesSumInteger   ' ?????
AverageChargeInteger = TotalChargesSumInteger / TotalSaleCountInteger
在“计数器”中添加“费用”似乎不正确。如果你想要平均值,不应该是:

TotalSaleCountInteger = SnowboardSaleCountInteger + WithBootsSaleCountInteger 
如果要在结果中使用诸如“xx.yy”之类的分数,
AverageChargeInteger
应为双精度或十进制,除非整美元可以用于赋值

如果需要从以前的单击中累加,则需要使用bootssuminteger将一些声明从过程中上移到
滑雪板sumInteger,以便它们可以累加。实际上,
使用bootssalecountinteger,TotalSaleCountInteger不做任何事情,并且始终为1。我想知道作为一个“销售计数器”,他们是否不应该增加文本框中的值,而不是仅仅增加1(我面前没有作业)

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

SnowboardPriceInteger = SnowboardInteger * SNOWBOARD_RENTAL_RATE
WithBootsPriceInteger = WithBootsInteger * WITH_BOOTS_RENTAL_RATE
TotalPriceInteger = SnowboardPriceInteger + WithBootsPriceInteger
您的常数是十进制的(
SNOWBOARD\u RENTAL\u RATE
),因此,计算结果必须进入十进制变量,但
SNOWBOARD PriceInteger
和其他变量不是。整数不能存储乘法或除法的小数结果

比如:

' 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  ;)  )


我想你只是把自己与太多的变量和旧尝试遗留下来的变量混淆了。

哪些变量不正确,所以我们可以缩小我们关注的范围Average ChargeInteger变量不正确,滑雪板SaleCountInteger和WithBootsSaleCountInteger的求和结果与预期不符。我将为每次计算创建一个函数。事实上,将每个抽象分解为单独的子对象或lambda。这将使您的代码更易于阅读和理解。我的另一个建议是提供一个例子,说明你得到的结果和应该得到的结果。使用double而不是integer。我想我会在下面的答案中处理你的编辑评论。TotalSaleCountInteger未正确处理,您需要将一些变量更改为十进制。我想您需要更多信息。这可能与整数数学有关。理想情况下,在混合整数时应该使用double out,而不是double out。如果将计算分解为函数,然后用示例进行注释,那就太好了,至少我们会有更多的细节。@Rebekah R.
ItemsSold
需要拆分为
BoardsSold
BootsSold
,这样您就可以显示它们了在总结中。打字时看不到我想要的所有内容。非常感谢,我一直在开发一个新的表单,试图避开那些糟糕的变量名。这对眼睛容易多了!我会让你知道,如果我这次让它工作的话。。。
' 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  ;)  )