-1
我無法弄清楚我的代碼有什麼問題。如果你有代碼,代碼應該使用成本,數量和促銷代碼計算物品的總成本。當我放入未使用的字符如!
時,它會一直崩潰。任何幫助或改進都會受到歡迎。語法錯誤 - 可計算
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
不能確定你是問這裏。語法錯誤會阻止編譯,但您似乎認爲您正在收到運行時錯誤。 –