try块VB.NET中的算术问题

时间:2016-11-29 16:31:51

标签: vb.net

我对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

2 个答案:

答案 0 :(得分:0)

用户可能会输入除整数之外的其他内容,因此请做好准备。你几乎做了 - 但不是很好。您考虑过考虑非整数。但是非数字输入怎么样,比如" abc"?对于所有情况,最好假设用户可能输入错误的输入并使用逻辑来检查它。您应该使用If..Else代替Try..Catch来处理该逻辑。

我也选择使用Mod,因为我无法弄清楚(b\0+1 = 1)应该做什么。 \对数字进行四舍五入,丢弃任何余数,以便不会帮助您。使用Decimal数据类型覆盖无穷小的剩余部分,即1.0000000000000000000000000001(但不能少。请参阅MSDN Decimal Data Type)。请注意D强制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()

答案 1 :(得分: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