我希望社区可以帮助我。我的程序遇到了一个小问题。只要用户在文本框中输入值,它就可以完美地工作。但是,当我在没有输入的情况下测试代码时,它仍会显示消息,就像用户丢失一样("请再次播放!")。此外,有时,它会显示一个错误,表示它无法转换" intResult = CInt(strBet)"。任何想法都表示赞赏。
' Declare public variables to call later.
Dim intA As Integer
Dim intB As Integer
Dim intC As Integer
Dim intResult As Integer
Dim rnd As New Random
Dim strBet As String
Dim intCount As Integer
Private Sub btnSpin_Click(sender As Object, e As EventArgs) Handles btnSpin.Click
' Variable to hold user input
strBet = txtBet.Text
' Generate random numbers to randomize PictureBox
intA = rnd.Next(1, 10)
intB = rnd.Next(1, 10)
intC = rnd.Next(1, 10)
' Check for numeric input.
If Not IsNumeric(strBet) Then
txtBet.Text = String.Empty
MessageBox.Show("Please enter a numeric value.")
txtBet.Focus()
Reset()
ElseIf txtBet.Text = String.Empty Then
btnSpin.Enabled = False
MessageBox.Show("Enter your bet." &
"Please use a numeric value.")
SlotTimer.Stop()
txtBet.Focus()
Reset()
' Reset the results, game timer, and counter to 0.
Else
intResult = 0
intCount = 0
SlotTimer.Start()
End If
' Check for matching values.
If intA = intB And intB = intC Then
intResult = CInt(strBet)
MessageBox.Show("Congratulations! You won " & (intResult * 3).ToString("c"))
txtBet.Focus()
ElseIf intA = intB Then
intResult = CInt(strBet)
MessageBox.Show("Congratulations! You won " & (intResult * 2).ToString("c"))
txtBet.Focus()
ElseIf intA = intC Then
intResult = CInt(strBet)
MessageBox.Show("Congratulations! You won " & (intResult * 2).ToString("c"))
txtBet.Focus()
ElseIf intB = intC Then
intResult = CInt(strBet)
MessageBox.Show("Congratulations! You won " & (intResult * 2).ToString("c"))
txtBet.Focus()
Else
MessageBox.Show("Please play again!")
txtBet.Focus()
End If
' Change images between spins.
changeImageA(picImageOne, 0)
changeImageA(picImageTwo, 0)
changeImageA(picImageThree, 0)
End Sub
Private Sub changeImageA(ByVal pic As PictureBox, ByVal int As Integer)
' Create function to control PictureBox with Integers
Select Case int
Case 1
pic.Image = My.Resources.Apple
Case 2
pic.Image = My.Resources.Banana
Case 3
pic.Image = My.Resources.Cherries
Case 4
pic.Image = My.Resources.Grapes
Case 5
pic.Image = My.Resources.Lemon
Case 6
pic.Image = My.Resources.Lime
Case 7
pic.Image = My.Resources.Orange
Case 8
pic.Image = My.Resources.Pear
Case 9
pic.Image = My.Resources.Strawberry
Case 10
pic.Image = My.Resources.Watermelon
End Select
End Sub
Private Sub SlotTimer_Tick(sender As Object, e As EventArgs) Handles SlotTimer.Tick
' Call counter and increase it by one.
intCount += 1
' Randomize the integer count to change picture images.
Randomize()
If intCount = 1 Then
changeImageA(picImageOne, intA)
ElseIf intCount = 3 Then
changeImageA(picImageTwo, intB)
ElseIf intCount = 5 Then
changeImageA(picImageThree, intC)
SlotTimer.Stop()
btnSpin.Enabled = True
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
' Close the form
Me.Close()
End Sub
Private Sub txtBet_TextChanged(sender As Object, e As EventArgs) Handles txtBet.TextChanged
' Allows user to press the "Enter" key instead of click "Spin".
Me.AcceptButton = btnSpin
End Sub
结束班