如何为vb.net制作2个双精度和整数异常

时间:2018-02-23 09:15:58

标签: vb.net

我已经输入了重量和高度的输入,并且消息出来了。我想尝试捕捉年龄,它是整数,系统知道它是从年龄。

例如,我输入age =“k”。然后消息框会说“年龄错误输入”。 然后当我输入重量时,高度=“a”。然后消息框会说“错误的输入高度”或“错误的输入重量”或“错误的输入hiehgt和重量”

如何喜欢这个例外?

    Private Sub ButtonCalculate_Click(sender As Object, e As EventArgs) Handles ButtonCalculate.Click
    Dim peopleName As String
    Dim peopleAge As Integer
    Dim peopleWeight As Double
    Dim peopleHeight As Double
    Dim peopleBMI As Double

    If Me.TextBoxWeight.Text <> "" Or Me.TextBoxHeight.Text <> "" Or Me.TextBoxAge.Text <> "" Then
        Try
            Me.peopleWeight = Double.Parse(Me.TextBoxWeight.Text)
            Me.peopleHeight = Double.Parse(Me.TextBoxWeight.Text)
            Me.peopleAge = Integer.Parse(Me.TextBoxAge.Text)
        Catch
            'this one is for weight/height
            MessageBox.Show("Wrong Input for Weight and Height", "Note", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            'Catch for age is here?

        End Try

    End If
End Sub

3 个答案:

答案 0 :(得分:2)

您检查值的方法并不好。你可以遵循这个想法:

    If Integer.TryParse(TextBoxAge.Text, peopleAge) = False Then
        MessageBox.Show("wrong input for age")
        Exit Sub
    End If

答案 1 :(得分:2)

捕获异常是混乱且昂贵的。请改用Double.TryParse()Integer.TryParse()

TryParse()的第二个参数是您希望在解析成功时将结果数设置为的变量。

If Double.TryParse(Me.TextBoxWeight.Text, Me.peopleWeight) = False Then
    MessageBox.Show("Invalid weight!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Return 'Do not continue.
End If

If Double.TryParse(Me.TextBoxHeight.Text, Me.peopleHeight) = False Then
    MessageBox.Show("Invalid height!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Return 'Do not continue.
End If

If Integer.TryParse(Me.TextBoxAge.Text, Me.peopleAge) = False Then
    MessageBox.Show("Invalid age!", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Return 'Do not continue.
End If

'Do your stuff here...

答案 2 :(得分:2)

一般情况下,如果可以的话,最好避免引发异常。正是出于这个原因,有一些TryParse方法可以将字符串解析为数字。

此外,如果用户未根据需要输入值,则可以存储消息并将其全部显示在一个框中,而不是最多显示三个消息框。为了帮助用户,最好告诉他们预期的内容。

作为存储错误消息的副产品,很容易测试输入有效值的时间,因此知道何时处理数据,如下所示:

Private Sub ButtonCalculate_Click(sender As Object, e As EventArgs) Handles ButtonCalculate.Click
    Dim peopleName As String
    Dim peopleAge As Integer
    Dim peopleWeight As Double
    Dim peopleHeight As Double
    Dim peopleBMI As Double

    Dim errorMessages As New List(Of String)

    If Not Double.TryParse(TextBoxWeight.Text, peopleWeight) Then
        errorMessages.Add("Please enter a number of kg for the weight, e.g. 60.5")
    End If

    If Not Double.TryParse(TextBoxHeight.Text, peopleHeight) Then
        errorMessages.Add("Please enter a number for the height in cm, e.g. 165")
    End If

    If Not Integer.TryParse(TextBoxAge.Text, peopleAge) Then
        errorMessages.Add("Please enter a whole number for the age, e.g. 35")
    End If

    If errorMessages.Count > 0 Then
        MessageBox.Show(String.Join(vbCrLf, errorMessages), "Note", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Else
        peopleBMI = peopleWeight / (peopleHeight * peopleHeight / 10000)
        MessageBox.Show("The BMI is " & peopleBMI.ToString("N1"))
    End If

End Sub

我将其编码为以厘米为单位的高度,取出转换因子10000并在条目以米为单位时调整错误消息。

对于满分,您还应检查合理的值。