If btnTotalCost.Text = "Calculate total cost" Then
btnTotalCost.Text = "Refresh"
Else
btnTotalCost.Text = "Calculate total cost"
End If
Dim dHamSandwich As Decimal = 1.35
Dim sHamQuantity As Single
txtHam.Text = sHamQuantity
dTotalSandwichCost = dHamSandwich * sHamQuantity
If btnTotalCost.Text = "Refresh" Then
MsgBox(dTotalSandwichCost)
End If
当我计算出现的消息框显示数字0.我希望它将数量和成本相乘以得出三明治的总成本。例如,如果我在数量文本框中输入2,则应将1.35乘以2.
答案 0 :(得分:0)
首先,您应该完全指定您使用的内容。正如Ste Griffiths的评论所暗示的那样,它似乎是VB.NET。
其次请开始学习数据类型和变量的使用。
你做
Dim dHamSandwich As Decimal = 1.35 ' <- this is fine
Dim sHamQuantity As Single '<- this is an uninitialised Single variable, in .net it will be set to 0
txtHam.Text = sHamQuantity '<- then you assign the unassigned Single variable to your Textbox's property Text(which requires a string)
'That means it will implicitly convert the Single to String and then "show" it in the textbox (which will be 0)
dTotalSandwichCost = dHamSandwich * sHamQuantity 'here you multiply 1.35 * 0. Sure it will be 0. Also you multiply a decimal with a single data type which will implicitly convert the latter (if I remember correctly)
这里的问题是。为什么你甚至使用Single和Decimal?你可以使用任何一个。但主要问题实际上甚至不是数据类型,而是赋值。