Visual Basic 2013 My input validation works when I enter a number less than 5 or
ID: 3679604 • Letter: V
Question
Visual Basic 2013
My input validation works when I enter a number less than 5 or a number greater than 25, but when I leave the input box blank the program crashes. It also crashes when I enter letter characters. I thought I could fix that with isNumeric(remainingSticks) but that does not seem to be working. What am I doing wrong?
Option Strict On
Public Class frmPickUpSticks
Dim playersTurn As Boolean = False
Dim remainingSticks As Integer 'count sticks
Dim losses As Integer = 0 'count player losses
Private Sub btnNewGame_Click(sender As Object, e As EventArgs) Handles btnNewGame.Click
lblOutput.Text = ""
remainingSticks = CInt(InputBox("How many matchsticks would you like (5 - 25)?", "Pick Number of Matches!"))
'Validate input
If IsNumeric(remainingSticks) Then
If (remainingSticks >= 5) And (remainingSticks <= 25) Then
DisplayMatches()
If remainingSticks Mod 4 = 1 Then
MessageBox.Show("You go first!")
playersTurn = True
turns()
Else
MessageBox.Show("I go first.")
turns()
End If
Else
MessageBox.Show("Please enter a number between 5 and 25.")
End If
Else
MessageBox.Show("Input must be numeric.", "Input Error")
End If
End Sub
Private Sub turns()
If playersTurn = True Then
ifPlayersTurn()
Else
If (remainingSticks Mod 4) = 0 Then
MessageBox.Show("There are " & remainingSticks & " sticks in the pile. I select 3.")
remainingSticks -= 3
ElseIf (remainingSticks Mod 4) = 3 Then
MessageBox.Show("There are " & remainingSticks & " sticks in the pile. I select 2.")
remainingSticks -= 2
Else
MessageBox.Show("There are " & remainingSticks & " sticks in the pile. I select 1.")
remainingSticks -= 1
End If
DisplayMatches()
'Show computer wins
If Not winnerCheck() Then
playersTurn = True
ifPlayersTurn()
Else
MessageBox.Show("I win!")
losses += 1
btnOne.Enabled = False
btnTwo.Enabled = False
btnThree.Enabled = False
End If
End If
End Sub
Private Sub ifPlayersTurn()
'Enable buttons
btnOne.Enabled = True
btnTwo.Enabled = True
btnThree.Enabled = True
End Sub
Private Function winnerCheck() As Boolean
If remainingSticks = 1 Then
Return True
Else
Return False
End If
End Function
Private Sub DisplayMatches()
lblOutput.Text = ""
For i As Integer = 0 To remainingSticks - 1
lblOutput.Text += "| "
Next
End Sub
Private Sub btnGiveRules_Click(sender As Object, e As EventArgs) Handles btnGiveRules.Click
MessageBox.Show("These are the rules of the game:" & Chr(13) & "The user chooses the number of matchsticks" & _
"(from 5 to 25) to place in a pile. Then, the computer chooses who will go first. At each turn, " & _
"the contestant can remove one, two, or three matchsticks from the pile. The contestant who removes" & _
" the last matchstick loses.", "Rules of the Game")
End Sub
Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
Close()
End Sub
Private Sub btnOne_Click(sender As Object, e As EventArgs) Handles btnOne.Click
remainingSticks -= 1
DisplayMatches()
playersTurn = False
turns()
End Sub
Private Sub btnTwo_Click(sender As Object, e As EventArgs) Handles btnTwo.Click
remainingSticks -= 2
DisplayMatches()
playersTurn = False
turns()
End Sub
Private Sub btnThree_Click(sender As Object, e As EventArgs) Handles btnThree.Click
remainingSticks -= 3
DisplayMatches()
playersTurn = False
turns()
End Sub
Private Sub btnSummary_Click(sender As Object, e As EventArgs) Handles btnSummary.Click
MessageBox.Show("You have lost " & losses & " game(s).", "Summary")
End Sub
End Class
Explanation / Answer
Private Sub btnNewGame_Click(sender As Object, e As EventArgs) Handles btnNewGame.Click
lblOutput.Text = ""
remainingSticks = CInt(InputBox("How many matchsticks would you like (5 - 25)?", "Pick Number of Matches!"))
'Validate input
if remainingSticks<>Null then
If IsNumeric(remainingSticks) Then
If (remainingSticks >= 5) And (remainingSticks <= 25) Then
DisplayMatches()
If remainingSticks Mod 4 = 1 Then
MessageBox.Show("You go first!")
playersTurn = True
turns()
Else
MessageBox.Show("I go first.")
turns()
End If
Else
MessageBox.Show("Please enter a number between 5 and 25.")
End If
Else
MessageBox.Show("Input must be numeric.", "Input Error")
End If
else
MessageBox.Show("Input must not be blank.", "Input Error")
end if
End Sub
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.