为什么要跳过代码中的else部分?

时间:2016-09-23 03:01:05

标签: vb.net facebook-like

'要求:编写一个名为CalculateTotalCost的Visual Basic过程,该过程从txtQuantity和txtUnitCost TextBox控件中读取用户输入的数据。 CalculateTotalCost过程应将在两个TextBox控件中输入的文本转换为数字。然后应将两个数字加在一起,根据订购数量应用适当的折扣,并在lblTotalCost Label控件中显示结果。 以下错误检查规则适用: 一个。用户在txtQuantity TextBox控件中输入的文本必须表示非负整数。如果没有,则该过程应在lblTotalCost Label控件中输出短语“Invalid quantity!”,不应进行进一步处理。 湾用户在txtUnitCost TextBox控件中输入的文本必须表示非负Double。如果没有,该过程应在lblTotalCost Label控件中输出短语“Invalid unit cost!”,不应进行进一步处理。 假设没有用户输入错误,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

1 个答案:

答案 0 :(得分:2)

因为你的第一个if条件是&lt; = 9.这包括所有负整数。