我无法弄清楚我的代码有什么问题。该代码应该使用成本,数量和促销代码来计算项目的总成本(如果您有)。当我放入!
等未使用的字符时,它会一直崩溃。欢迎任何帮助或改进。
Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim decDisplayTotal As Decimal
Dim decPrice As Decimal = txtPrice.Text
Dim intQuantity As Integer = txtQuantity.Text
Dim strPromoCode As String = txtPromoCode.Text
decDisplayTotal = decPrice * intQuantity
lblDisplayTotal.Text = "$" & decDisplayTotal
If decPrice < 0 Then
lblDisplayTotal.Text = ("")
txtPrice.Text = Nothing
txtQuantity.Text = Nothing
txtPromoCode.Text = Nothing
MessageBox.Show("Please enter an appropriate price.", "Invalid Input")
End If
If intQuantity < 0 Then
lblDisplayTotal.Text = ("")
txtPrice.Text = Nothing
txtQuantity.Text = Nothing
txtPromoCode.Text = Nothing
MessageBox.Show("Please enter an approriate quantity.", "Invalid Input")
End If
If strPromoCode = ("132") Then
MessageBox.Show("You used a limited time, 10% off code! Watch your price drop 10%!", "10% off")
decDisplayTotal = 0.9 * (decPrice * intQuantity)
lblDisplayTotal.Text = "$" & decDisplayTotal
End If
If strPromoCode = ("129") Then
MessageBox.Show("You used a limited time, 20% off code! Watch your price drop 20%!", "20% off")
decDisplayTotal = 0.8 * (decPrice * intQuantity)
lblDisplayTotal.Text = "$" & decDisplayTotal
End If
If strPromoCode = ("136") Then
MessageBox.Show("You used a limited time, 30% off code! Watch your price drop 30%!", "30% off")
decDisplayTotal = 0.7 * (decPrice * intQuantity)
lblDisplayTotal.Text = "$" & decDisplayTotal
End If
If strPromoCode = ("264") Then
MessageBox.Show("You used a limited time, buy 1 get 1 free code, so watch your total cut in half!", "Buy 1 Get 1 Free")
decDisplayTotal = 0.5 * (decPrice * intQuantity)
lblDisplayTotal.Text = "$" & decDisplayTotal
End If
If strPromoCode = ("125") Then
decDisplayTotal = (decPrice * intQuantity)
lblDisplayTotal.Text = "$" & decDisplayTotal
End If
Try
decPrice = Convert.ToInt16(txtPrice.Text)
Catch ex As Exception
lblDisplayTotal.Text = Nothing
MessageBox.Show("Please enter an acceptable price.", "Invalid Input")
txtPrice.Text = Nothing
End Try
Try
intQuantity = Convert.ToInt16(txtQuantity.Text)
Catch ex As Exception
lblDisplayTotal.Text = Nothing
MessageBox.Show("Please enter an acceptable quanitity.", "Invalid Input")
txtQuantity.Text = Nothing
End Try
Try
strPromoCode = Convert.ToInt16(txtPromoCode.Text)
Catch ex As Exception
lblDisplayTotal.Text = Nothing
MessageBox.Show("Please enter a valid Promo Code.", "Invalid Input")
txtPromoCode.Text = Nothing
End Try
End Sub
Private Sub txtPrice_TextChanged(sender As Object, e As EventArgs) Handles txtPrice.TextChanged
lblDisplayTotal.Text = ("")
End Sub
Private Sub txtQuantity_TextChanged(sender As Object, e As EventArgs) Handles txtQuantity.TextChanged
lblDisplayTotal.Text = ("")
End Sub
Private Sub txtPromoCode_TextChanged(sender As Object, e As EventArgs) Handles txtPromoCode.TextChanged
lblDisplayTotal.Text = ("")
End Sub
End Class
答案 0 :(得分:1)
要清楚理解的第一件事是字符串不是数字。 VB允许这种松弛,但它以许多微妙的方式咬回来。 我假设您将单点放在任何文本框中,然后尝试使用该TEXT,因为它是一个字符串。有时它不会有效。
正确的方法是让框架尝试转换,如果失败则通知您的用户该问题。
因此,不使用各种Convert.ToXXXXx而是使用SomeType.TryParse(I.E. Int32.TryParse)
Dim decPrice As Decimal
Dim intQuantity As Integer
Dim strPromoCode As String
if Not Decimal.TryParse(txtPrice.Text, decPrice) Then
MessageBox.Show("Please type a valid number for Price")
ClearInputs()
return
End if
if Not Int32.TryParse(txtPrice.Text, intQuantity) Then
MessageBox.Show("Please type a valid number for Quantity")
ClearInputs()
return
End if
Private Sub ClearInputs()
lblDisplayTotal.Text = ""
txtPrice.Text = ""
txtQuantity.Text = ""
txtPromoCode.Text = ""
End Sub
现在您输入的值存储在正确的数据类型变量中,您可以继续执行代码的其余部分.....
....
decDisplayTotal = decPrice * intQuantity
lblDisplayTotal.Text = "$" & decDisplayTotal.ToString
....
您需要为项目设置的重要配置是项目属性中的Option Strict On
。此配置将禁止字符串和数字之间的隐式转换,并强制您编写更正确的代码。
顺便说一句,在检查strPromoCode之后,您不需要再次重复将文本框中的字符串转换为相应变量的过程