Dim mynumber as Integer 'This is the variable as an integer, (Integers only allow WHOLE numbers)
Console.WriteLine("Enter your number..") 'Ask to enter number
Try
mynumber = Console.ReadLine 'Read user input and store it
Catch
Console.WriteLine()
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine("Only whole numbers allowed! Press enter to start again!")
Console.ResetColor()
Console.ReadLine()
Console.Clear()
GoTo start
End Try
好的,从上面的示例中可以看出,我设置了一个 Try/Catch 用于错误处理。我遇到了一个问题。是的 Try/Catch 代码阻止输入字母(字符串),但是当我输入十进制数字时,它仍然接受它。为什么?如何防止这种情况发生?十进制数不应该被接受,因为整数只接受整数!
谢谢。