我正在开发一个需要用户输入的程序。我试图拒绝他们的输入,如果它是小数或字母。我该怎么做?
Function GetHowLongToRun() As Integer
Dim Years As Integer
Dim valid As Boolean = False
Console.WriteLine("Welcome to the Plant Growing Simulation")
Console.WriteLine()
Console.WriteLine("You can step through the simulation a year at a time")
Console.WriteLine("or run the simulation for 0 to 5 years")
Console.WriteLine("How many years do you want the simulation to run?")
Do
Console.Write("Enter a number between 0 and 5, or -1 for stepping mode: ")
Years = Console.ReadLine()
Try
If Years > -2 And Years < 6 Then
valid = True
Else
Console.WriteLine("Invalid input. Enter a whole number between 0 and 5, or -1 for stepping mode: ", Years)
End If
Catch ex As Exception
Console.WriteLine("Invalid input. Enter a whole number between 0 and 5, or -1 for stepping mode: ")
GetHowLongToRun()
End Try
Loop Until valid
Return Years
End Function
答案 0 :(得分:0)
这里有多个问题:
Console.ReadLine()
将总是返回一个string
,但它会立即分配给一个Integer
值。完全编译起来表明极端的糟糕做法,其中Option Strict
已关闭。最好使用Integer.Parse()/TryParse()
,Convert.ToInt32()
或CInt()
中的任何一个。要解决此问题,可以在异常处理程序中删除多余的GetHowLongToRun()
调用。该循环将确保代码再次尝试。但是我会更进一步,以一种更通用的方式将输入隔离到它自己的函数中:
Public Function ReadInteger(ByVal Prompt As String, ByVal InvalidPrompt As String, ByVal Retries As Integer, Optional ByVal MinValue As Integer = 0, Optional ByVal MaxValue As Integer = Integer.MaxValue)
Dim attemptCount As Integer = 0
While attemptCount < Retries
Console.Write(Prompt)
Dim input As As String = Console.ReadLine()
Dim result As Integer
If Integer.TryParse(input, result) Then
If result >= MinValue AndAlso result <= MaxValue Then
return result
End If
End If
Console.WriteLine(InvalidPrompt)
attemptCount += 1
End While
Throw New InvalidOperationException()
End Function
然后您可以像这样从原始对象调用新函数:
Function GetHowLongToRun() As Integer
Console.WriteLine("Welcome to the Plant Growing Simulation")
Console.WriteLine()
Console.WriteLine("You can step through the simulation a year at a time")
Console.WriteLine("or run the simulation for 0 to 5 years")
Console.WriteLine("How many years do you want the simulation to run?")
Return ReadInteger("Enter a number between 0 and 5, or -1 for stepping mode: ", "Invalid input.", 3, -1, 5)
End Function
答案 1 :(得分:0)
您可以使用正则表达式检查输入是否符合您的要求。这很方便,因为您可以在一个语句中检查多个条件。
您首先需要包含正则表达式库:
Imports System.Text.RegularExpressions
然后创建一个正则表达式对象,将所需的模式指定为构造函数参数:
Dim regex As Regex = New Regex("\d+")
此正则表达式模式 \ d + 将标识具有1个或多个连续数字的字符串。正则表达式功能强大,您可以根据需要将模式修改为更具体。在线上有很多文档。
使用Regex对象的'Match'方法,然后可以测试字符串以查看它们是否与指定的模式匹配。这将返回“匹配”类型的对象:
Dim match as Match = regex.Match(Years)
最后,使用match对象的'Success'属性检查匹配是否成功。
If match.Success Then
valid = True
End If
或者,您可以将输入字符串解析为整数。如果解析失败,则表明它不是有效的输入:
If Integer.TryParse(Years) Then
valid = True
End If
答案 2 :(得分:0)
注释和解释
Dim Years As String = Console.ReadLine()
Dim HowLongToRun As Integer = GetHowLongToRun(Years)
Function GetHowLongToRun(input As String) As Integer
Dim valid As Boolean = False
Dim ValidYears As Integer
Do
'.TryParse not only tests for a valid number and returns True or False but
'if it finds a valid number it will assign it to the second parameter
'The AndAlso portions of the If will not be evaluated unless the first portion is True.
'This is called short-circuiting.
If Int32.TryParse(input, ValidYears) AndAlso ValidYears < 6 AndAlso ValidYears > -2 Then
valid = True
Else
'You are misusing the (String, Object) overload of Console.WriteLine
'This is meant as a sort of variation on String.Format where the second parameter
'is substituted for a place holder in the string like {0}
'Since there is no placeholder, I deleted the , Years
Console.WriteLine("Invalid input. Enter a whole number between 0 and 5, or -1 for stepping mode: ")
input = Console.ReadLine
End If
Loop Until valid
Return ValidYears
End Function