0

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

例如:我输入 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
4

1 回答 1

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

向“柜台”添加“费用”似乎不正确。如果你想要平均值不应该是:

TotalSaleCountInteger = SnowboardSaleCountInteger + WithBootsSaleCountInteger 

如果您想要结果中的“xx.yy”等分数,则AverageChargeInteger应该是 Double 或 Decimal,除非整个美元都可以用于分配。

如果您需要从以前的点击中累积,那么您需要将一些声明从过程中移出,SnowboardSumInteger, WithBootsSumInteger以便它们可以累积。实际上,WithBootsSaleCountInteger, TotalSaleCountInteger不要做任何事情并且始终为 1。我想知道作为“SaleCounter”,它们是否不应该增加文本框中的值而不仅仅是 1(我面前没有分配)。

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

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

高温高压

我认为您只是将自己与太多变量以及旧尝试遗留下来的变量混淆了。

于 2013-10-07T00:44:53.177 回答