价格仅显示为0.00美元

时间:2017-11-05 18:06:51

标签: vb.net winforms

我正在尝试制作一个菜单来计算最终的总费用,但最终费用应该显示在文本框中,但它只能显示0.00美元,任何人都可以提供帮助。它应该显示为您选择主菜的总成本以及客户选择的多少面和/或浇头。参见附件。

Public Class frmMenu
    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        'Set variables
        Dim decCBurger As Decimal = 11D
        Dim decChicken As Decimal = 7.49D
        Dim decFries As Decimal = 5.99D
        Dim decOnionRings As Decimal = 5.99
        Dim decSalad As Decimal = 5.99
        Dim decSoup As Decimal = 5.99
        Dim decFP As Decimal = 5.99
        Dim decPickle As Decimal = 0.99
        Dim decOnion As Decimal = 0.99
        Dim decTomato As Decimal = 0.99
        Dim decBacon As Decimal = 0.99
        Dim decEgg As Decimal = 0.99
        Dim decTotalCost As Decimal
        Dim decSides As Decimal
        Dim decTop As Decimal
        Dim decMain As Decimal

        'Display cost and label 
        txtTotalCost.Text = decTotalCost.ToString("C")
        decTotalCost = decTop + decSides + decMain

        ' Check for main dish
        If radCBurger.Checked Then
            decMain = decCBurger
        ElseIf radChicken.Checked Then
            decMain = decChicken
        End If
        'Check for side dishes
        If chkFries.Checked Then
            decSides = decFries
        End If
        If chkOnionRings.Checked Then
            decSides = decOnionRings
        End If
        If chkFP.Checked Then
            decSides = decFP
        End If
        If chkSoup.Checked Then
            decSides = decSoup
        End If
        If chkSalad.Checked Then
            decSides = decSalad
        End If
        'Check for Toppings
        If chkOnion.Checked Then
            decTop = decOnion
        End If
        If chkTomato.Checked Then
            decTop = decTomato
        End If
        If chkBacon.Checked Then
            decTop = decBacon
        End If
        If chkEgg.Checked Then
            decTop = decEgg
        End If
        If chkPickle.Checked Then
            decTop = decPickle
        End If

1 个答案:

答案 0 :(得分:1)

这应该在你的sub的末尾,你颠倒了这些线。你不能指望计算机知道你想要什么。你在开始时计算所以你的总数总是0.00。

 'Display cost and label 
        decTotalCost = decTop + decSides + decMain
        txtTotalCost.Text = decTotalCost.ToString("C")

所以喜欢这个

Public Class frmMenu
    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        'Set variables
        Dim decCBurger As Decimal = 11D
        Dim decChicken As Decimal = 7.49D
        Dim decFries As Decimal = 5.99D
        Dim decOnionRings As Decimal = 5.99
        Dim decSalad As Decimal = 5.99
        Dim decSoup As Decimal = 5.99
        Dim decFP As Decimal = 5.99
        Dim decPickle As Decimal = 0.99
        Dim decOnion As Decimal = 0.99
        Dim decTomato As Decimal = 0.99
        Dim decBacon As Decimal = 0.99
        Dim decEgg As Decimal = 0.99
        Dim decTotalCost As Decimal
        Dim decSides As Decimal
        Dim decTop As Decimal
        Dim decMain As Decimal

        ' Check for main dish
        If radCBurger.Checked Then
            decMain = decCBurger
        ElseIf radChicken.Checked Then
            decMain = decChicken
        End If
        'Check for side dishes
        If chkFries.Checked Then
            decSides = decFries
        End If
        If chkOnionRings.Checked Then
            decSides = decOnionRings
        End If
        If chkFP.Checked Then
            decSides = decFP
        End If
        If chkSoup.Checked Then
            decSides = decSoup
        End If
        If chkSalad.Checked Then
            decSides = decSalad
        End If
        'Check for Toppings
        If chkOnion.Checked Then
            decTop = decOnion
        End If
        If chkTomato.Checked Then
            decTop = decTomato
        End If
        If chkBacon.Checked Then
            decTop = decBacon
        End If
        If chkEgg.Checked Then
            decTop = decEgg
        End If
        If chkPickle.Checked Then
            decTop = decPickle
        End If


        'Display cost and label 
        decTotalCost = decTop + decSides + decMain
        txtTotalCost.Text = decTotalCost.ToString("C")


end sub

end class