2013-10-07 88 views
0

我已經開始了,除了累計總數中的2個以外,所有內容都按照預期工作,計算不按照我需要的方式工作。而不是增加以前的銷售總額它釘在新的銷售結束...VB.NET - 積累數字和計算平均值

例如:我輸入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 
+1

哪些變量不正確,所以我們可以縮小我們關注的範圍 – Plutonix

+0

AverageChargeInteger變量不正確,並且SnowboardSaleCountInteger和WithBootsSaleCountInteger未按預期相加 –

+1

我會爲每次計算創建一個函數。事實上,將每個抽象分解爲一個單獨的子或lambda。這會讓你的代碼更容易閱讀理解。我的另一個建議是提供一個例子,說明你得到了什麼結果以及它們應該是什麼。 – jcwrequests

回答

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應該是一個雙人間或小數,除非整個美元是確定的分配。

如果您需要通過先前的點擊進行累計,那麼您需要將某些聲明從程序中移出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 

Click事件:

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

HTH

我覺得你只是混淆自己有太多的變量和那些從舊的嘗試遺留下來的。

+0

我認爲你需要更多信息。它可能與整數數學有關。理想情況下,應該使用雙打而不是混合整數。 – jcwrequests

+0

那也是,但平均數學看起來不對 – Plutonix

+0

如果計算被分解爲函數然後用例子評論,那麼至少我們會有更多的細節,這將是很好的。 – jcwrequests