我应该添加一个TRY-CATCH,以确保用户输入的是数字值而不是字母值。当您输入字母并显示“有错误,请输入数字”时,它工作正常,但程序停止并显示错误:
这是我的代码
将intPoints设置为整数
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
txtPoints.Clear()
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
End
End Sub
Private Sub btnReward_Click(sender As Object, e As EventArgs) Handles btnReward.Click
Try
intPoints = txtPoints.Text
intPoints = Convert.ToInt32(txtPoints.Text)
Catch ex As Exception
Finally
MessageBox.Show("There was an error, please enter digits")
End Try
'intPoints = Convert.ToInt32(txtPoints.Text)
If txtPoints.Text <= 0 Then
MessageBox.Show("You cannot have less than 0")
End If
If txtPoints.Text = 1 Then
MessageBox.Show("Keep Trying")
End If
If txtPoints.Text = 2 Then
MessageBox.Show("Keep Trying")
End If
If txtPoints.Text = 3 Then
MessageBox.Show("Keep Trying")
End If
If txtPoints.Text = 4 Then
MessageBox.Show("Keep Trying")
End If
If txtPoints.Text = 5 Then
MessageBox.Show("You're Gettomg close, just a few more!")
End If
If txtPoints.Text = 6 Then
MessageBox.Show("You're Gettomg close, just a few more!")
End If
If txtPoints.Text = 7 Then
MessageBox.Show("You're Gettomg close, just a few more!")
End If
If txtPoints.Text = 8 Then
MessageBox.Show("You're Gettomg close, just a few more!")
End If
If txtPoints.Text = 9 Then
MessageBox.Show("Almost, just one more!")
End If
If txtPoints.Text = 10 Then
MessageBox.Show("Awesome! You earned a reward!")
End If
If txtPoints.Text >= 11 Then
MessageBox.Show("You cannot have more than 10 points")
End If
End Sub
' A computer uses
结束班级