Visual Basic小于0

时间:2014-10-02 15:01:12

标签: vb.net

我试图制作一个程序,当我把一个小于0的值放在一个像“  不能使用负数我有这个

Public Class Form1 'Sebastian roman. Perimeter, 10/1/2014


    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

        Try
            Dim intSide1 As Integer = txtSide1.Text
            Dim intSide2 As Integer = txtSide2.Text
            Dim intSide3 As Integer = txtSide3.Text
            Dim intSide4 As Integer = txtSide4.Text
            Dim intTotal As Integer = intSide1 + intSide2 + intSide3 + intSide4
            lblMessage.Text = intTotal.ToString("#,###.##")
        Catch ex As Exception
            MessageBox.Show("Incorrect Input. Enter a numeric value.")
        End Try

    End Sub
End Class

是的,我必须使用try catch方法,我需要帮助

2 个答案:

答案 0 :(得分:2)

这听起来很像家庭作业,但你真的需要正确转换你的整数,然后进行实际的比较。

Public Class Form1 'Sebastian roman. Perimeter, 10/1/2014

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

        Try
           ' Relies on the GetIntegerInput method to throw exceptions
           ' for invalid entries
            Dim intSide1 As Integer = GetIntegerInput(txtSide1.Text)
            Dim intSide2 As Integer = GetIntegerInput(txtSide2.Text)
            Dim intSide3 As Integer = GetIntegerInput(txtSide3.Text)
            Dim intSide4 As Integer = GetIntegerInput(txtSide4.Text)
            Dim intTotal As Integer = intSide1 + intSide2 + intSide3 + intSide4
            lblMessage.Text = intTotal.ToString("#,###.##")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Function GetIntegerInput(ByVal input as String) As Integer)
        Dim returnValue as Integer

        ' Will attempt a proper try parse. AndAlso will short circuit
        ' the comparison so a failure in TryParse will not perform the
        ' the second evaluation. In either case, an actual exception is
        ' thrown with your invalid numeric message 
        If (Not Int32.TryParse(input, returnValue) AndAlso returnValue < 0) Then
            Throw New ArgumentException("Incorrect input. Enter a proper numeric value.")
        End If

        Return returnValue
    End Function
End Class

答案 1 :(得分:0)

整数可以是负数......

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

    Try
        Dim intSide1 As Integer = txtSide1.Text
        Dim intSide2 As Integer = txtSide2.Text
        Dim intSide3 As Integer = txtSide3.Text
        Dim intSide4 As Integer = txtSide4.Text
        Dim intTotal As Integer = intSide1 + intSide2 + intSide3 + intSide4
        lblMessage.Text = intTotal.ToString("#,###.##")
        //New code
        if intSide1 < 0 or intSide2 < 0 or intSide3 < 0 intSide4 < 0 Then
         MessageBox.Show("Incorrect Input. Negative number not valid")
        end if
    Catch ex As Exception
        MessageBox.Show("Incorrect Input. Enter a numeric value.")
    End Try



End Sub