我的IF语句和Is Numeric出了什么问题

时间:2014-08-30 09:44:34

标签: vb.net if-statement isnumeric

以下代码揭示了我理解中的差距。有人可以告诉我它是什么以及如何修复代码

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim NumberToGuess, Answer As Integer
    NumberToGuess = InputBox("Enter a Secret Number Between 1 and 20!")
    While Answer <> NumberToGuess
        Answer = InputBox("Please enter your guess")
        If IsNumeric(Answer) = False Then MsgBox("That ain't no number")
        If Answer > NumberToGuess Then MsgBox("Too high thicko. Try Again")
        If Answer < NumberToGuess Then MsgBox("Too Low chump. Try Again")
    End While
    MsgBox("Well done you guessed the right number")
End Sub

4 个答案:

答案 0 :(得分:2)

这个问题有很多问题,你的问题是关于你在谈论哪一个问题。所以我会在这里列出所有这些。

  1. 您将NumberToGuessAnswer都声明为整数,并为其指定InputBox的结果。但是InputBox可以返回任何内容(数字或字母)。一旦您尝试将用户的输入分配给NumberToGuess,它就会出错。那是在你检查它是否是数字之前。
  2. 如果您有OPTION STRICT ON,它将显示编译错误“Option Strict On禁止从'String'到'Integer'的隐式转换。”保持OPTION STRICT ON是一个不错的选择一般的练习,有助于避免无辜的错误。例如在这里,您要将String类型分配给Integer变量,但不允许这样做。
  3. 您使用While循环InputBox。除非他们给出正确答案,否则用户无法取消游戏。 InputBox的“取消”按钮不起作用。
  4. 将评估您的所有If条件,而不管之前的条件。我假设您希望一次只显示一个消息框。所以你可能也想使用ElseIf
  5. 要解决问题,请转到:

    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
    

    上面的代码是一个很大的烦恼因为MessageBox,然后是InputBox, then MessageBox , then InputBox ... 要解决此问题,您可以在InputBox本身显示消息。

    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
    

答案 1 :(得分:1)

没什么大不了的 - 你只是错过了一些else;)

private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim NumberToGuess, Answer As Integer
    NumberToGuess = InputBox("Enter a Secret Number Between 1 and 20!")
    While Answer <> NumberToGuess
        Answer = InputBox("Please enter your guess")
        If IsNumeric(Answer) = False Then MsgBox("That ain't no number")
        Else
            If Answer > NumberToGuess Then MsgBox("Too high thicko. Try Again")
            If Answer < NumberToGuess Then MsgBox("Too Low chump. Try Again")
        End If
    End While
    MsgBox("Well done you guessed the right number")

End Sub

问题在于,如果您只是这样写,则检查第一个条件 - 如果您没有输入数字,则会显示消息并检查您的下一个条件 - 您显然只希望检查这些条件是否为一个号码。所以将它包装在Else

中 BTW:我不认为Answer的缺失值是个问题,但我现在无法检查(我的Linux发行版上的My MonoDevelop与VB.net存在一些问题:( - 调试代码以查找如果是请的话)

答案 2 :(得分:1)

问题是InputBox function的返回值 它返回一个字符串,并尝试将其分配给整数变量 这会强制VB编译器执行从字符串到整数的隐式转换,直到您键入有效数字为止,但是如果您将输入框保留为空,则隐式转换将失败并引发InvalidCastException

如果使用Option Strict On编译代码,则会在构建期间捕获此代码 如果没有它,你的程序会在作业中崩溃 NumberToGuess

的分配中存在同样的问题
Dim NumberToGuess, Answer As Integer
While True
    Dim input = InputBox("Enter a Secret Number Between 1 and 20!")
    if IsNumeric(input) Then
        NumberToGuess = Convert.ToInt32(input)
        Exit While
    Else
        MsgBox("Not a valid number")
    End If
End While    
While Answer <> NumberToGuess
    Dim result = InputBox("Please enter your guess")
    If IsNumeric(result) = False Then 
       MsgBox("That ain't no number")
    Else
       Answer = Convert.ToInt32(result)
       If Answer > NumberToGuess Then MsgBox("Too high thicko. Try Again")
       If Answer < NumberToGuess Then MsgBox("Too Low chump. Try Again")
    End If
End While
MsgBox("Well done you guessed the right number")

我还应该补充一点,Mr Carsten Konig的答案正确指向逻辑代码流中的错误。

答案 3 :(得分:0)

您可以使用:

 Dim NumberToGuess, Answer As Integer
        NumberToGuess = InputBox("Enter a Secret Number Between 1 and 20!")
        While Answer <> NumberToGuess
            Answer = InputBox("Please enter your guess")
            If IsNumeric(Answer) = False Then
                MsgBox("That ain't no number")
            Else
                Select Case Answer < NumberToGuess
                    Case True
                        MsgBox("Too Low chump. Try Again")
                    Case False
                        MsgBox("Too high thicko. Try Again")
                    Case Else
                        MsgBox("Well done you guessed the right number")
                End Select
            End If
        End While