VB.net价格计算

时间:2012-09-13 20:32:01

标签: vb.net

我正在申请显示订单的总价。它根据订购的商品数量以及客户是批发商还是零售商来决定价格。

计算使用:

1-4项=批发商每件10美元。

5件物品=批发商每件9美元。

1-3项=零售商每件15美元。

4-8项=零售商每件$ 14。

9+ item =每件商品12美元(如果是零售商)。 我坚持计算,并对如何做到这一点感到困惑。

GUI完整,这是我到目前为止:

图片GUI:http://i.imgur.com/P2tDz.png

Dim quantity As Decimal
Dim price As Decimal
Dim wholesaler As Integer
Dim retailer As Integer

Integer.TryParse(txtUnits.Text, price)

chkRetailer.Text = retailer
chkWholesale.Text = wholesaler


If wholesaler Then
    If Quantity <= 4 Then
        Price = 10
    Else
        Price = 9
    End If
ElseIf retailer Then
    If Quantity <= 3 Then
        Price = 15
    ElseIf Quantity <= 8 Then
        Price = 14
    Else
        Price = 12
    End If
End If

lblTotPrice.Text = price * quantity

lblTotPrice.Text = price.ToString("C0")

这是我很困惑的计算,我相信显示器和声明是正确的。

谢谢。

4 个答案:

答案 0 :(得分:2)

使用Case statements会更清楚(提供的链接的VB示例非常有帮助)

首先使用IF语句确定您是否是批发商,并确定两个案例陈述中的哪一个分配有效价格

答案 1 :(得分:0)

如果你按如下方式嵌套if循环会更简单。 (我在这里使用伪代码。)

If Wholesaler Then
  If Quantity <= 4 Then
    Price = 10
  Else
    Price = 9
  EndIf
ElseIf Retailer Then
  If Quantity <= 3 Then
    Price = 15
  ElseIf Quantity <= 8 Then
    Price = 14
  Else
    Price = 12
  EndIf
End If

TotalCost = Price * Quantity

答案 2 :(得分:0)

这里有很多错误。

此声明从txtUnits获取输入并将其分配给价格。

Integer.TryParse(txtUnits.Text, price)

这两行除了为复选框的text属性赋值0外什么也没做。

chkRetailer.Text = retailer
chkWholesale.Text = wholesaler

这些语句将始终评估为False,因为它们始终等于0.价格永远不会被分配。

If wholesaler Then
  'never reaches here
ElseIf retailer Then
  'or here
End If

您正在对标签进行两次分配。这一个

lblTotPrice.Text = price * quantity

没用,因为它会立即被

取代
lblTotPrice.Text = price.ToString("C0")

答案 3 :(得分:0)

我认为你做得对,但是你分配给lblTotPrice价格*数量,然后分配价格。试试这最后两行

Dim subTotal as integer
subtotal = price * quantity 
lblTotPrice.Text = subtotal.ToString("C0") 

还有一些其他可能有帮助的事情,例如使用Select Case语句会在代码中更好一点,但我认为这是你的主要问题。

相关问题