'VORAUSSETZUNGEN: Schreiben Sie eine Visual Basic-Prozedur namens CalculateTotalCost, die vom Benutzer eingegebene Daten aus den TextBox-Steuerelementen txtQuantity und txtUnitCost liest. Die CalculateTotalCost-Prozedur sollte den in den zwei TextBox-Steuerelementen eingegebenen Text in Zahlen konvertieren. Es sollte dann die beiden Zahlen zusammen multiplizieren, den entsprechenden Rabatt basierend auf der bestellten Menge anwenden und das Ergebnis im lblTotalCost Label-Steuerelement anzeigen. Die folgenden Fehlerprüfregeln gelten: a. Der Text, den der Benutzer im Steuerelement txtQuantity TextBox eingegeben hat, muss eine nicht negative Ganzzahl darstellen. Wenn dies nicht der Fall ist, sollte die Prozedur den Ausdruck "Ungültige Menge!" Im lblTotalCost Label-Steuerelement ausgeben und es sollte keine weitere Verarbeitung stattfinden. b. Der Text, den der Benutzer im TextBox-Steuerelement txtUnitCost eingegeben hat, muss ein nicht negatives Double darstellen. Wenn dies nicht der Fall ist, sollte die Prozedur die Phrase "Invalid unit cost!" Im lblTotalCost Label-Steuerelement ausgeben und es sollte keine weitere Verarbeitung stattfinden. Wenn keine Benutzereingabefehler vorliegen, sollte die korrekt abgezinste Summe, die im lblTotalCost Label-Steuerelement angezeigt wird, im aktuellen Format angezeigt werden. Die Anzeige sollte ein führendes Währungssymbol enthalten (abhängig davon, wie der Computer eingerichtet wurde, ist dies wahrscheinlich ein Dollarzeichen) und genau zwei nachgestellte Ziffern nach dem Dezimaltrennzeichen.Warum überspringt der else Teil meines Codes?
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
Wenn Sie Breakpoint und Debug auf der IF, was ist intQuantity gleich? –