2016-09-23 124 views
-1

'需求:編寫一個名爲CalculateTotalCost的Visual Basic過程,該過程從txtQuantity和txtUnitCost TextBox控件讀取用戶輸入的數據。 CalculateTotalCost過程應該將在兩個TextBox控件中輸入的文本轉換爲數字。然後,它應該將這兩個數字相乘,根據訂購的數量應用適當的折扣,並將結果顯示在lblTotalCost Label控件中。 適用以下錯誤檢查規則: a。用戶在txtQuantity TextBox控件中輸入的文本必須表示一個非負Integer。如果沒有,程序應該在lblTotalCost Label控件中輸出短語「Invalid quantity!」,並且不應該進行進一步的處理。 b。用戶在txtUnitCost TextBox控件中輸入的文本必須表示一個非負Double。否則,過程應在lblTotalCost標籤控件中輸出短語「無效單位成本!」,並且不應進行進一步處理。 假設沒有用戶輸入錯誤,應該以當前格式顯示lblTotalCost Label控件中顯示的正確折扣總額。顯示屏應該包含一個主要的貨幣符號(取決於計算機的設置方式,這可能是一個美元符號),並且包含小數點後的兩位數字。爲什麼要跳過我的代碼的其他部分?

Public Class Form1 

    Private Sub lblTotalCost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblTotalCost.Click 

     'Author: Eric Konga_ 14200694 _BCIT/3_ The Papaua New Guinea University of Technology 

     ' this program will read read user entered data from the two text boxes on the form and 
     ' will calcualte (Multiply) the two numbers together and will then apply the appropriate discount 
     'based on the quantity ordered, and display the result(Total Cost) in the Label control. 

     'Declaring Variables as strings. This sets will out put to the screen the appropriate percentages 
     'based the quantity ordered. 
     Dim strDiscount As String 
     Dim strDiscount1 As String 
     Dim strDiscount2 As String 
     Dim strDiscount3 As String 
     Dim strDiscount4 As String 

     'declaring variables as integer, double and long. this sets of program will output to the screen 
     ' 
     Dim intQuantity As Integer 
     Dim dblUnitCost As Double 
     Dim dblTotalCost As Double 

     'Assigning Variables 
     strDiscount = "0%" 
     strDiscount1 = "20%" 
     strDiscount2 = "30%" 
     strDiscount3 = "40%" 
     strDiscount4 = "50%" 

     ' This is a mathematical calculator that calculates the TotalCost (TC). 
     intQuantity = txtQuantity.Text 
     dblUnitCost = txtUnitCost.Text 
     dblTotalCost = intQuantity * dblUnitCost 

     If intQuantity <= 9 Then 
      lblTotalCost.Text = "The Total Cost is: $" & String.Format("{0:n2}", dblTotalCost) & " and it's " & strDiscount & _ 
       " Discount." 


     ElseIf intQuantity <= 19 Then 
      lblTotalCost.Text = "The Total Cost is: $" & String.Format("{0:n2}", dblTotalCost) & " and it's " & strDiscount1 & _ 
       " Discount." 


     ElseIf intQuantity <= 49 Then 
      lblTotalCost.Text = "The Total Cost is: $" & String.Format("{0:n2}", dblTotalCost) & " and it's " & strDiscount2 & _ 
       " Discount." 

     ElseIf intQuantity <= 99 Then 
      lblTotalCost.Text = "The Total Cost is: $" & String.Format("{0:n2}", dblTotalCost) & " and it's " & strDiscount3 & _ 
       " Discount." 

     ElseIf intQuantity >= 100 Then 
      lblTotalCost.Text = "The Total Cost is: $" & String.Format("{0:n2}", dblTotalCost) & " and it's " & strDiscount4 & _ 
       " Discount." 

      ' under this condition, it will only execute if the integer(QTY) is negative or 
      'the unser entered float(UC) is negative. 

     Else 
      lblTotalCost.Text = (" Invalid Quantity!" & " or Ivalid Unit Cost!") 


     End If 


    End Sub 
End Class 
+0

當你在IF上斷點和調試時,intQuantity等於什麼? –

回答

2

因爲您的第一個條件是< = 9.這包括所有負整數。

+0

謝謝。我沒有意識到這個簡單的錯誤。希望這一次,它應該工作 –

相關問題