2011-05-02 145 views
0

我正在編寫一個控制檯應用程序,需要用戶輸入某些值。我想禁止任何字母輸入。控制檯自動寫入「從字符串」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 

前端模塊

+0

你能顯示你的代碼嗎?這條消息是如何產生的?你只是將例外轉儲到控制檯窗口? – mellamokb 2011-05-02 16:51:42

+0

@mellamokb我發佈了我的代碼,我不確定消息是如何生成的,但它顯示出來。 – 2011-05-02 17:04:19

回答

1
Dim inputString = Console.ReadLine 
If Integer.TryParse(inputString, num) Then 
    DoSomething(num) 
Else 
    Console.WriteLine("Not a valid number, please try again.") 
End If 

下面是做這件事,尊重你的要求:

Module Module1 
Sub Main() 
    System.Console.WriteLine("Input valid numbers seperated by spaces to have them mathematically averaged.") 
    Dim inputArray As String() = System.Text.RegularExpressions.Regex.Replace(Console.ReadLine().Trim(), "\s{2,}", " ").Split(New Char() {" "}) 
    Dim values As New ArrayList 
    Dim sum As Integer 
    For i As Integer = 0 To inputArray.Length - 1 
     Try 
      sum = sum + Integer.Parse(inputArray(i), Globalization.NumberStyles.Integer) 
      values.Add(inputArray(i)) 
     Catch ex As Exception 
      Console.WriteLine(String.Format("The value ""{0}"" is not a valid number and will be ignored. ExceptionMessage: {1}", inputArray(i), ex.Message)) 
     End Try 
    Next 
    Dim average As Decimal = sum/values.Count 
    Console.WriteLine(vbCrLf) 
    Console.WriteLine(String.Format("The average of ""{0}"" is {1}", Join(values.ToArray, ", "), average)) 
    Console.WriteLine(vbCrLf) 
    Main() 
End Sub 

前端模塊

+0

我怎樣才能將其納入我的代碼?我嘗試過,但我不確定如何使它工作。 – 2011-05-02 17:55:54

+0

這個答案避免了在現實世界中可能是理想的例外,但是這是'家庭作業' – Michael 2011-05-02 20:45:53

+0

根據您的要求更新了我的答案 – Till 2011-05-03 12:52:10

0

那麼對於初學者,在Try - Catch應該是裏面的輸入採集For循環。由於您的程序是現在編寫的,只要輸入了一個錯誤的值,就會跳轉到catch並且程序結束!如果您嘗試將輸入轉換爲小數,則不需要手動拋出異常,如果輸入不是小數,則會自動拋出!嘗試將當前控制檯輸入作爲小數轉換,然後將該數字添加到列表中。

Dim inputNumber as Decimal = CDec(Console.ReadLine()) 
Input.Add(inputNumber) 

而且n將相同數量的每一次,所以你是怎麼做的,它不會工作(查找列表是如何工作的基本知識,如何元素添加到列表以及如何顯示列表元素)

+0

當我把「如果輸入= Integer.MaxValue」程序將正確運行,我所需要的是讓我的異常消息顯示。 – 2011-05-02 21:04:39

+0

'if input = Integer.MaxValue'是無稽之談。用所有有效的數字嘗試你的程序,你會發現你的程序不正確。用你最新的代碼更新你的問題,然後按照我給你的建議。 – Michael 2011-05-02 21:43:31

+0

我嘗試添加,但我認爲我沒有做對。 – 2011-05-02 22:54:12

相關問題