There are many problems in this question and your question is vague about which one you are talking about. So I'll list all of them here.
- You declare both
NumberToGuess
and Answer
as Integer and assign it the result of an InputBox
. But InputBox
can return anything (number or alpha). And it will error out as soon as you try to assign the user's input to NumberToGuess
. And that's before you check whether it is number or not.
- If you have
OPTION STRICT ON
it will show you compilation error "Option Strict On disallows implicit conversions from 'String' to 'Integer'". Keeping OPTION STRICT ON
is a good practice in general and helps avoid innocent looking mistakes. e.g. Here you are assigning String
type to Integer
variable, which it doesn't allow.
- You have used a
While
loop with InputBox
. There is no way for user to cancel out of the game unless they give the correct answer. The Cancel button of InputBox won't work.
- All your
If
conditions will be evaluated irrespective of previous one. I assume you want only one of the Message Boxes to be shown at a time. So you may want to make use of ElseIf
too.
To fix the problems, here we go:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim NumberToGuess As String '<-- declare as string because we will hold the result of InputBox in it.
Dim Answer As String = "" '<-- declare as string because we will hold the result of InputBox in it.
NumberToGuess = InputBox("Enter a Secret Number Between 1 and 20!")
While Answer <> NumberToGuess
Answer = InputBox("Please enter your guess")
If String.IsNullOrEmpty(Answer) Then Exit While '<-- if user pressed cancel button in InputBox.
If Not IsNumeric(Answer) Then
MsgBox("That ain't no number")
ElseIf CInt(Answer) > CInt(NumberToGuess) Then
MsgBox("Too high thicko. Try Again")
ElseIf CInt(Answer) < CInt(NumberToGuess) Then
MsgBox("Too Low chump. Try Again")
Else
' neither less nor more. so this is the correct answer.
MsgBox("Well done you guessed the right number")
Exit While
End If
End While
End Sub
The above code however is a big annoyance because a MessageBox
, then InputBox, then
MessageBox, then
InputBox...
To fix this, you can show the message in the InputBox
itself.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim NumberToGuess As String '<-- declare as string because we will hold the result of InputBox in it.
Dim Answer As String = "" '<-- replace with whatever answer you are expecting.
Dim Message As String = "Please enter your guess"
NumberToGuess = InputBox("Enter a Secret Number Between 1 and 20!")
While Answer <> NumberToGuess
Answer = InputBox(Message)
If String.IsNullOrEmpty(Answer) Then Exit While '<-- if user pressed cancel button in InputBox.
If Not IsNumeric(Answer) Then
Message = "That ain't no number"
ElseIf CInt(Answer) > CInt(NumberToGuess) Then
Message = "Too high thicko. Try Again"
ElseIf CInt(Answer) < CInt(NumberToGuess) Then
Message = "Too Low chump. Try Again"
Else
' neither less nor more. so this is the correct answer.
MsgBox("Well done you guessed the right number")
Exit While
End If
End While
End Sub