2016-11-29 37 views
-1

我對VB.NET和編程一般都比較陌生,所以我仍然在學習語法的基礎知識。在一個try塊中的算術問題VB.NET

我不斷收到寫着

語句的結束預計

上線(b\0+1=1)錯誤。我將如何結束這個解決錯誤?謝謝你的回覆!

b = InputBox("Please Enter Radius.") 'enter radius 
    Try (b\0+1 = 1) 
    Exit Try 
    Catch ex As NullReferenceException 
    Console.WriteLine("Not a whole number. Please try again") 
    Console.ReadLine() 
     Exit Try 
    End Try 
+1

Try/Catch定義了一個塊 - 所有的代碼都應該放在裏面。你不能指定一個聲明來嘗試這種方式,如果這就是你正在嘗試做的事 – Plutonix

+0

你將b除以零,你不能這樣做。你究竟想要做什麼? –

+0

您還需要啓用Option Strict。 InputBox返回一個字符串,你(顯然)試圖對它進行數學運算。我不知道你想用'(b \ 0 + 1 = 1)'來做什麼,但它錯了。你需要分配結果,但是你應用的任何測試都是錯誤的。你真的不需要嘗試/趕上 – Plutonix

回答

0

「期望的結束語」意味着您給出的代碼行比期望的更多。在這種情況下,這是因爲Try必須在自己的路線上。

對於確定用戶是否輸入整數的問題,將文本轉換爲整數的有用方法是Int32.ParseInt32.TryParse方法。如果出現錯誤,前者會拋出異常,而後者將返回布爾值,指示解析是否成功。

「NullReferenceException」不是您想要捕捉的內容:您應該查閱可能拋出異常的方法的文檔,以查看它可以拋出哪些異常。有時,它足以趕上任何例外:

Module Module1 

    Sub Main() 
     Dim b As String 
     Dim isGoodNumber As Boolean = True 
     Dim radius As Integer 

     ' OPTION ONE: Decline any bad input with a generic error message. 
     Do 
      b = InputBox("Please enter the radius as a whole number:") 
      Console.WriteLine("You entered {0}", b) 
      Try 
       radius = Integer.Parse(b) 
       isGoodNumber = True 
      Catch ex As Exception 
       isGoodNumber = False 
      End Try 

      If Not isGoodNumber Then 
       Console.WriteLine("Please enter a whole number.") 
      End If 

     Loop Until isGoodNumber 

     Console.ReadLine() 

    End Sub 

End Module 

如果要執行取決於被拋出什麼樣的錯誤不同的操作,您可以使用多種Catch條款:

Module Module1 

    Sub Main() 
     Dim b As String 
     Dim isGoodNumber As Boolean = True 
     Dim radius As Integer 

     ' OPTION TWO: Decline any bad input with a more specific error message. 
     Do 
      b = InputBox("Please enter the radius as a whole number:") 
      Console.WriteLine("You entered {0}", b) 
      Try 
       radius = Integer.Parse(b) 
       isGoodNumber = True 
      Catch ex As ArgumentNullException 
       isGoodNumber = False 
       Console.WriteLine("Please enter a whole number.") 
      Catch ex As FormatException 
       isGoodNumber = False 
       Console.WriteLine("Please enter a whole number.") 
      Catch ex As OverflowException 
       isGoodNumber = False 
       Console.WriteLine("Please enter a whole number between {0} and {1}", Integer.MinValue, Integer.MaxValue) 
      End Try 

     Loop Until isGoodNumber 

     Console.ReadLine() 

    End Sub 

End Module 

在這種情況下,如果您不需要提供錯誤的輸入相關錯誤消息,則甚至不需要拋出異常的可能性:

Module Module1 

    Sub Main() 
     Dim b As String 
     Dim isGoodNumber As Boolean = True 
     Dim radius As Integer 

     ' OPTION THREE: A shorter way of option one. 
     Do 
      b = InputBox("Please enter the radius as a whole number:") 
      Console.WriteLine("You entered {0}", b) 
      isGoodNumber = Integer.TryParse(b, radius) 
      If Not isGoodNumber Then 
       Console.WriteLine("Please enter a whole number.") 
      End If 

     Loop Until isGoodNumber 

     Console.ReadLine() 

    End Sub 

End Module 
0

用戶可能會輸入除整數之外的其他東西,所以要做好準備。你幾乎做到了 - 但不是很好。你以爲會計是一個非整數。但是非數字輸入如「abc」呢?對於所有情況,最好假定用戶可能輸入錯誤的輸入並使用邏輯來檢查它。您應該使用If..Else而不是Try..Catch來處理該邏輯。

我也選擇使用Mod,因爲我無法弄清楚(b\0+1 = 1)應該做什麼。 \四捨五入數字,丟棄任何餘數,所以不會幫助你。使用Decimal數據類型來覆蓋無限小的剩餘部分,即1.0000000000000000000000000001(但不能少於,見MSDN Decimal Data Type)。請注意0​​以強制Mod操作員的操作數。

' I added declaration of 'b' here, assuming it's a string since InputBox returns string. 
Dim b As String 
b = InputBox("Please Enter Radius.") 
Console.WriteLine("You entered {0}", b) 
Dim radius As Decimal 
If Decimal.TryParse(b, radius) Then ' True if it's a number 
    If radius Mod 1D = 0D Then ' True if it has no remainder 
     Console.WriteLine("You entered an integral number. Good job") 
    Else 
     Console.WriteLine("Not a whole number. Please try again") 
    End If 
Else 
    Console.WriteLine("Not even a number. Please try again") 
End If 
Console.ReadLine()