我正在制作一个简单的汽车商店会计软件,其中包含3 checkboxes
附加配件的汽车面板和group box of 3 radiobutton
汽车细节控件即可。我有base price textbox
和calculate button
。
我有一张额外配件的预定成本和
- 立体声系统= $ 40,000
- 皮革内饰= $ 150,000
- 计算机导航= $ 100,000
- 标准= 无需额外费用
- Pearlized = $ 30,000
- 自定义详细信息= $ 50,000
我刚刚使用了一种简单的Sum方法,但是我收到了一个错误:
未处理的类型' System.InvalidCastException'发生了 在Microsoft.VisualBasic.dll
中其他信息:从字符串转换""输入' Double'是 无效。
以下是运行的代码:
Private Sub Calculate_Click(sender As Object, e As EventArgs) Handles Calculate.Click
Dim Sum As Integer
' Sum of Standard Detailing + Stereo system + Base Price
If Rad1.Checked + Chk1.Checked + basePrice.Text Then
Sum = "40,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Standard Detailing + Leather Interior + Base Price
ElseIf Rad1.Checked + Chk2.Checked + basePrice.Text Then
Sum = "150,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Standard Detailing + Computer Navigation + Base Price
ElseIf Rad1.Checked + Chk3.Checked + basePrice.Text Then
Sum = "100,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Pearlized Detailing + Stereo system + Base Price
ElseIf Rad2.Checked + Chk1.Checked + basePrice.Text Then
Sum = "30,000" + "40,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Pearlized Detailing + Leather Interior + Base Price
ElseIf Rad2.Checked + Chk2.Checked + basePrice.Text Then
Sum = "30,000" + "150,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Pearlized Detailing + Computer Navigation + Base Price
ElseIf Rad2.Checked + Chk3.Checked + basePrice.Text Then
Sum = "30,000" + "100,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Customised Detailing + Stereo system + Base Price
ElseIf Rad3.Checked + Chk1.Checked + basePrice.Text Then
Sum = "50,000" + "40,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Customised Detailing + Leather Interior + Base Price
ElseIf Rad3.Checked + Chk2.Checked + basePrice.Text Then
Sum = "50,000" + "150,00"
' Sum of Customised Detailing + Computer Navigation+ Base Price
ElseIf Rad3.Checked + Chk3.Checked + basePrice.Text Then
Sum = "50,000" + "100,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Standard Detailing + Stereo system + Leather Interior + Base Price
ElseIf Rad1.Checked + Chk1.Checked + Chk2.Checked + basePrice.Text Then
Sum = "40,000" + "150,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Standard Detailing + Stereo system + Leather Interior + Computer Navigation+ Base Price
ElseIf Rad1.Checked + Chk1.Checked + Chk2.Checked + Chk3.Checked + basePrice.Text Then
Sum = "40,000" + "150,000" + "100,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Standard Detailing + Stereo system + Computer Navigation + Base Price
ElseIf Rad1.Checked + Chk1.Checked + Chk3.Checked + basePrice.Text Then
Sum = "40,000" + "100,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Standard Detailing + Leather Interior + Computer Navigation + Base Price
ElseIf Rad1.Checked + Chk2.Checked + Chk3.Checked + basePrice.Text Then
Sum = "100,000" + "150,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Pearlized Detailing + Stereo system + Leather Interior + Base Price
ElseIf Rad2.Checked + Chk1.Checked + Chk2.Checked + basePrice.Text Then
Sum = "30,000" + "40,000" + "150,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Pearlized Detailing + Stereo system + Leather Interior + Computer Navigation+ Base Price
ElseIf Rad2.Checked + Chk1.Checked + Chk2.Checked + Chk3.Checked + basePrice.Text Then
Sum = "30,000" + "40,000" + "150,000" + "100,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Pearlized Detailing + Stereo system + Computer Navigation + Base Price
ElseIf Rad2.Checked + Chk1.Checked + Chk3.Checked + basePrice.Text Then
Sum = "30,000" + "40,000" + "100,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Pearlized Detailing + Leather Interior + Computer Navigation + Base Price
ElseIf Rad2.Checked + Chk2.Checked + Chk3.Checked + basePrice.Text Then
Sum = "30,000" + "100,000" + "150,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Customised Detailing + Stereo system + Leather Interior + Base Price
ElseIf Rad2.Checked + Chk1.Checked + Chk2.Checked + basePrice.Text Then
Sum = "50,000" + "40,000" + "150,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Customised Detailing + Stereo system + Leather Interior + Computer Navigation+ Base Price
ElseIf Rad2.Checked + Chk1.Checked + Chk2.Checked + Chk3.Checked + basePrice.Text Then
Sum = "50,000" + "40,000" + "150,000" + "100,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Customised Detailing + Stereo system + Computer Navigation + Base Price
ElseIf Rad2.Checked + Chk1.Checked + Chk3.Checked + basePrice.Text Then
Sum = "50,000" + "40,000" + "100,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
' Sum of Customised Detailing + Leather Interior + Computer Navigation + Base Price
ElseIf Rad2.Checked + Chk2.Checked + Chk3.Checked + basePrice.Text Then
Sum = "50,000" + "100,000" + "150,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
End If
规则是客户可以购买任何或所有配件的汽车,但他/她必须只选择一(1)种类型/形式的汽车细节。
我哪里错了。请帮忙谢谢。
答案 0 :(得分:0)
行。只是一些问题:)
您的所有数字“30,000”“40,000”等都是字符串的开头。
使用Val生成了一个double类型,如果你的文本框为空,那么你的帖子就会出现错误。如果没有值输入以旧换新仍然输入0
此外,您的If语句使用“ + ”而不是和,这将产生逻辑错误。
同样在处理金钱时,你绝不应该使用整数
在默认编译器设置中,绝不应将Option Strict设置为Off的代码编写。编写Option Strict设置为On的代码要困难一些,但它可以减少程序运行时出错的可能性
而不是像这样的代码: -
If Rad1.Checked + Chk1.Checked + basePrice.Text>"" Then
Sum = "40,000" + Val(tradeIn.Text) + Val(basePrice.Text)
subTotal.Text = Sum
你应该使用这个
Dim Sum As Single
' Sum of Standard Detailing + Stereo system + Base Price
If Rad1.Checked And chk1.Checked And BasePrice.Text > "" Then
Sum = 40000 + CSng(TradeIn.Text) + CSng(BasePrice.Text)
subTotal.Text = Sum.ToString
但说实话(没有冒犯),有很多重复,可能有很多方法可以缩短它 - 我的建议是
Private Sub Calculate_Click(sender As Object, e As EventArgs) Handles Calculate.Click
Dim Sum As Single
Dim Base As Single = CSng(BasePrice.Text)
Dim Trade As Single = CSng(TradeIn.Text)
Dim Stereo As Single = 0
Dim LeatherInt As Single = 0
Dim CompNav As Single = 0
Dim Pearlized As Single = 0
Dim CustomDetail As Single = 0
If chk1.Checked Then Stereo = 40000
If chk2.Checked Then LeatherInt = 150000
If chk3.Checked Then CompNav = 100000
If Rad2.Checked Then Pearlized = 30000
If Rad3.Checked Then CustomDetail = 50000
Sum = Base + Trade + Stereo + LeatherInt + CompNav + Pearlized + CustomDetail
subTotal.Text = Sum.ToString()
End Sub
并且不要忘记在您的代码中添加检查,以确保基本价格和以旧换新价格的数字实际上是数字。当你在文本框中输入数字时,不要使用这样的逗号 50,000 ,只需输入 50000