为什么这些计算不正确

时间:2017-05-19 23:43:30

标签: vb.net

我正在为我的大学作业创建一个披萨订单计划,我不得不计算订单的总价,我的变量如下:

现在无论出于何种原因,打印到我的某个表单的总价格。使用:

Form7.lblRecieptDetails.Text = ("Price: " & globalVariables.orderTotal & vbNewLine & "Quantity: " & globalVariables.pizzaCount & vbNewLine & "Toppings :" & globalVariables.toppingCount) 

是不正确的,但据我所见,我无法看到任何错误,所以一定是计算背后的逻辑。

    Public Shared toppingCount As Double = 0.0
    Public Shared pizzaCount As Double = 0.0
    Public Shared pizzaSize As String = ""
    Public Shared orderDetails As String = ""
    Public Shared pizzabasePrice As Double = 0
    Public Shared toppingPrice As Double = 0
    Public Shared oneTopping As Double = 1.5
    Public Shared twoTopping As Double = 3.0
    Public Shared threeTopping As Double = 4.5
    Public Shared small As Double = 6.0
    Public Shared medium As Double = 8.0
    Public Shared large As Double = 10.0
    Public Shared overallPizzaPrice As Double = 0.0
    Public Shared orderTotal As Double = 0.0
    Public Shared toppingTotal As Double = 0.0

以下是我用来验证条件并计算价格和其他所有内容的代码:(我对vb很新,所以我理解代码中会出现错误和错误处理)

 If comboPizzaSize.Text = "Small" Or comboPizzaSize.Text = "small" Then
        If comboToppingCount.Text = "1" Then
            If txtPizzaCount.Text >= 15 Then
                If InputBox("Enter manager password") = "Management" Then
                    globalVariables.pizzaCount = txtPizzaCount.Text
                    globalVariables.pizzabasePrice = globalVariables.small
                    globalVariables.toppingCount = 1
                    globalVariables.orderTotal = globalVariables.oneTopping + globalVariables.pizzabasePrice * globalVariables.pizzaCount
                    If MsgBox("Show Reciept?") = DialogResult.OK Then
                        Me.Hide()
                        Form7.Show()
                        If globalVariables.orderTotal >= 20 Then
                            globalVariables.orderTotal = globalVariables.orderTotal * 0.95
                        ElseIf globalVariables.orderTotal < 20 Then
                            globalVariables.orderTotal = globalVariables.orderTotal
                        End If
                    End If
                End If
            ElseIf txtPizzaCount.Text < 15 Then
                globalVariables.pizzaCount = txtPizzaCount.Text
                globalVariables.pizzabasePrice = globalVariables.small
                globalVariables.toppingCount = 1
                globalVariables.orderTotal = globalVariables.toppingTotal + globalVariables.overallPizzaPrice
                If MsgBox("Show Reciept?") = DialogResult.OK Then
                    Me.Hide()
                    Form7.Show()
                    If globalVariables.orderTotal >= 20 Then
                        globalVariables.orderTotal = globalVariables.orderTotal * 0.95
                    ElseIf globalVariables.orderTotal < 20 Then
                        globalVariables.orderTotal = globalVariables.orderTotal
                    End If
                End If
            End If
        End If
    End If

有什么建议吗?在此先感谢您的帮助! :)

1 个答案:

答案 0 :(得分:0)

这种If / Else逻辑很难让任何人理解。最好将某些部分划分为“保护条款”,以便您可以更快地中止,嵌套级别不是那么深,重复次数更少,而且整体代码更少:

If CInt(txtPizzaCount.Text) >= 15 AndAlso InputBox("Enter manager password") <> "Management" Then Exit Sub
If comboToppingCount.Text <> "1" Then Exit Sub
If comboPizzaSize.Text.Trim().ToLower() <> "small" Then Exit Sub

globalVariables.pizzaCount = CInt(txtPizzaCount.Text)
globalVariables.pizzabasePrice = globalVariables.small
globalVariables.toppingCount = 1
globalVariables.orderTotal = (globalVariables.oneTopping + globalVariables.pizzabasePrice) * globalVariables.pizzaCount

If globalVariables.orderTotal >= 20 Then globalVariables.orderTotal = globalVariables.orderTotal * 0.95

If MsgBox("Show Reciept?") = DialogResult.OK Then
    Form7.ShowDialog()
End If

看看以这种方式遵循逻辑有多容易?

虽然我在这里:全球变量也不是那么好。最好为特定订单定义一个类,并传递对该类实例的引用。这可能是globalVariables在这里做的事情,但如果是这样,那就是一个糟糕的名字。