我正在编写一个控制台应用程序,需要用户输入某些值。我想禁止任何字母输入。控制台自动写入“从字符串”b“转换为键入”整数“无效。”但我希望控制台显示我的个人信息“不是有效的号码,请重试。”我怎样才能做到这一点?只有数字,不允许使用字母 - 用户输入
我试过很多不同的关键字和短语,但都没有工作。也许我做错了(不太可能)或者它只是为了别的东西。无论哪种方式,我需要帮助。
回顾:控制台中的用户输入只允许数字而不是字母,并且会显示我的消息。
我不想发布我的代码,因为人们总是选择它,但在这里。请注意,这必须是一个例外。这是作业,我需要一个例外,这是用户可以搞砸的一种方式。请不要告诉我不要使用例外。
Module Module1
Sub Main()
Try
System.Console.WriteLine("Input up to 10 valid numbers to have them mathematically averaged.")
For Index = 0 To 9
Dim Input As IList
Input = Console.ReadLine()
Next Index
If ' here is where I want to add that numbers only Then
Throw New exception("Not a valid number, please try again.")
Else
System.Console.WriteLine("Now averaging numbers...")
Dim average As Double = (n + n + n + n + n + n + n + n + n + n)/10
Console.WriteLine("The average of " & n & "," & n & "," & n & "," & n & "," & n & "," & n & "," & n & "," & n & "," & n & " and " & n & " is " & average & ".", "Calculation")
End If
Catch e As Exception
System.Console.WriteLine(e.Message)
End Try
End Sub
前端模块
你能显示你的代码吗?这条消息是如何产生的?你只是将例外转储到控制台窗口? – mellamokb 2011-05-02 16:51:42
@mellamokb我发布了我的代码,我不确定消息是如何生成的,但它显示出来。 – 2011-05-02 17:04:19