2014-11-08 16 views
1

我正在寫一個簡單的隨機數猜測遊戲,並使用語句isnumeric。這是在Visual Studio正在做一個Visual Basic程序2013年什麼可以使用,而不是在vb.net isnumeric

這裏是我的代碼:

Private Sub btnTry_Click(sender As Object, e As EventArgs) Handles btnTry.Click 
    Dim intTry As Integer 
    Dim strInputBox As String 
    strInputBox = InputBox("Enter Your Number to Guess.", "Input Needed") 

    If IsNumeric(strInputBox) Then 
     intTry = CInt(strInputBox) 
     lstTried.Items.Add(intTry) 
    Else 
     MessageBox.Show("Please Type Only Numbers") 
    End If 

    If intTry = intRandomNumber Then 
     MessageBox.Show("You Got It!", "Correct Guess") 

    Else 
     MessageBox.Show("Incorrect. Please try again.", "Incorrect Guess") 

    End If 

我想我如果語句中使用到位「則IsNumeric」的東西。我不確定該怎麼做。有人能幫我嗎。我試圖使用integer.tryparse,但無法使其工作。具體的幫助將不勝感激。這個工作現在正在進行中,我試圖讓它一個人留下,但我被告知這是舊式代碼,還有另一種方式來做到這一點。

感謝,

史蒂夫

+2

'Integer.TryParse'將取代'IsNumeric'和'CInt'線,唉...... – Plutonix 2014-11-08 18:56:38

+0

你有什麼問題Int32.TryParse? – Steve 2014-11-08 19:05:26

回答

2

你可以試試這個,我也做了一些提示更正你的代碼。

Private Sub btnTry_Click(sender As Object, e As EventArgs) Handles btnTry.Click 
Dim intTry As Integer = 0 

'This wont throw an exception if it's not an integer, it will come back false... 
If Integer.TryParse(InputBox("Enter your number to guess.", "Input Needed"), intTry) Then 
    lstTried.Items.Add(intTry) 
Else 
    MessageBox.Show("Please Type Only Numbers") 
    Exit Sub 
End If 

If intTry = intRandomNumber Then 
    MessageBox.Show("You Got It!", "Correct Guess") 
Else 
    MessageBox.Show("Incorrect. Please try again.", "Incorrect Guess") 
End If 

End Sub 
相關問題