我正在使用Integer.TryParse方法驗證用戶輸入是數字還是非數字在我的程序中。 1)如果用戶輸入是數字,程序將繼續並驗證用戶輸入的範圍是從0到9. 2)如果用戶輸入非數字輸入,程序將顯示消息「無效輸入「並要求用戶從頭開始。VB2008控制檯應用程序 - Integer.TryParse方法錯誤
以下是我的編碼: 子的Main()
Dim sevenNumbers As Integer()
sevenNumbers = New Integer(6) {}
Dim index As Integer
Dim number As Integer
Dim reEnter As Boolean = True
Console.WriteLine("Please enter 7 integers: ")
Console.WriteLine("<ATTENTION: FROM 0 TO 9 ONLY>")
Console.WriteLine()
While reEnter
For index = 0 To 6
Console.WriteLine("Please enter the integer no." & "{0}" & " : ", index + 1) 'Prompt user to enter 7 integers.
sevenNumbers(index) = Console.ReadLine() 'The 7 integers are stored in an array.
If Integer.TryParse(sevenNumbers(index), number) Then
While sevenNumbers(index) < 0 Or sevenNumbers(index) > 9
Console.WriteLine("<invalid input>")
Console.WriteLine()
Console.WriteLine("------------------------------------------")
Console.WriteLine("<Please re-enter the 7 integers>")
Console.WriteLine("------------------------------------------")
Console.WriteLine()
reEnter = True
Exit For
End While
Else
Console.WriteLine("<invalid input>")
Console.WriteLine()
Console.WriteLine("------------------------------------------")
Console.WriteLine("<Please re-enter the 7 integers>")
Console.WriteLine("------------------------------------------")
Console.WriteLine()
reEnter = True
Exit For
End If
reEnter = False
Next
End While
末次
然而,當用戶輸入一個非數字輸入,程序無法繼續,並顯示一個錯誤,被迫關閉。
我想這
子的Main() 昏暗NUM爲整數
Console.Write("enter num:")
Dim input = Console.ReadLine
If Integer.TryParse(input, num) Then
Console.WriteLine("valid. num = " & num)
Else
Console.WriteLine("invalid")
End If
End Sub
它的作品,我想知道我該編碼的一部分是錯誤的?
感謝您的幫助!
是的,我已經改變sevenNumbers爲字符串,而不是使用整數和它的作品!謝謝! – user1702495