首页 > 解决方案 > 运行程序后我需要帮助获取正确的值

问题描述

在 VB.NET 中,我需要:

当我运行程序并在输入售出票数和票价的值后点击提交时,小计和税金恢复正常。但是我总是在 Total Total 和 Daily Grand Total 中得到 0 的值。似乎无法弄清楚为什么。

这是我到目前为止所拥有的:

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    Dim intTicketsSold As Integer
    Dim dblTicketPrice As Double
    Const dblTax As Double = 0.07
    Dim dblOATotal As Double
    Dim dblSubTotal As Double
    Dim cdblDGTotal As Double
    Const cdblPromoProfit As Double = 0.2

    'Create local variables
    intTicketsSold = txtTicketsSold.Text
    dblTicketPrice = txtTicketPrice.Text
    lblTax.Text = dblTax
    lblOATotal.Text = dblOATotal
    lblSubTotal.Text = dblSubTotal
    lblDGTotal.Text = cdblDGTotal
    lblPromoProfit.Text = cdblPromoProfit

    'Calculate Subtotal
    lblSubTotal.Text = intTicketsSold * dblTicketPrice

    'calculate tax to subtotal
    lblTax.Text = 0.07 * (intTicketsSold * dblTicketPrice)

    'Get the overall total
    dblOATotal = dblTax + dblSubTotal

    'Add Total to Daily Grand Total to get Running Total

    cdblDGTotal = cdblDGTotal + dblOATotal

    'Calculate Promoters Profit of Daily Grand Total
    lblPromoProfit.Text = 0.2 * cdblDGTotal
End Sub

我究竟做错了什么?

标签: vb.net

解决方案


Your code is not wrong, but you've forgotten to replace the content of Overall Total label and Daily Grand Total label after calculating their values, and the result of intTicketsSold * dblTicketPrice should be saved in a variable (dblSubtotal) to use it in Overall Total calculation, that way your calculations will be correct. Change your code like below:

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    Dim intTicketsSold As Integer
    Dim dblTicketPrice As Double
    Const dblTax As Double = 0.07
    Dim dblOATotal As Double
    Dim dblSubTotal As Double
    Dim cdblDGTotal As Double
    Const cdblPromoProfit As Double = 0.2

    'Create local variables
    intTicketsSold = Convert.ToInt32(txtTicketsSold.Text)
    dblTicketPrice = Convert.ToDouble(txtTicketPrice.Text)
    lblTax.Text = dblTax
    lblSubTotal.Text = dblSubTotal

    lblPromoProfit.Text = cdblPromoProfit

    'Calculate Subtotal
    dblSubTotal = intTicketsSold * dblTicketPrice
    lblSubTotal.Text = dblSubTotal

    'calculate tax to subtotal
    lblTax.Text = 0.07 * (intTicketsSold * dblTicketPrice)

    'Get the overall total
    dblOATotal = dblTax + dblSubTotal
    lblOATotal.Text = dblOATotal
    'Add Total to Daily Grand Total to get Running Total

    cdblDGTotal = cdblDGTotal + dblOATotal
    lblDGTotal.Text = cdblDGTotal
    'Calculate Promoters Profit of Daily Grand Total
    lblPromoProfit.Text = 0.2 * cdblDGTotal
End Sub

推荐阅读