繁体   English   中英

VB.net <>数学函数

[英]VB.net <> math function

我正在尝试使以下VB.net数学函数正常工作。 目前,当我输入kpa阀> 7时,该数字应乘以1.5。 它只是产生相同的数字。 请注意,系统压力是由用户输入的。 我重新发布了完整代码,

Private Sub Calculate_Click(sender As Object, e As EventArgs) Handles Calculate.Click
    Try
        Dim kpa As Double = CDbl(SystemPressure.Text)
        If kpa > 0 AndAlso kpa <= 7 Then
            TestPressure.Text = kpa
        ElseIf kpa > 7.1 Then
            TestPressure.Text = 1.5 * kpa
        End If

        Dim dia = ComboBox1.Text
        Dim length As Double = CDbl(LengthMeters.Text)
        Dim area As Double = 3.142 * ((dia * 0.5 / 1000) ^ 2)
        Dim volume As Double = Math.Round(length * area, 2)
        Dim litres As Double = volume * 1000
        Dim minutes As Double = Math.Round((litres / 30) * 5, 0)
        Dim hours As Double = Math.Round((minutes / 60), 2)

        LabelVolume.Text = volume.ToString & " : Meters Cubed"
        TestPressure.Text = kpa & " : Kpa"
        TestTimeMinutes.Text = minutes.ToString & " : Minutes"
        TestTimeHours.Text = hours.ToString & " : Hours"

    Catch
        MessageBox.Show("Error: Enter numbers only", "Error")
        LengthMeters.Clear()
        SystemPressure.Clear()
        ComboBox1.Focus()
    End Try

End Sub

Private Sub clear_Click(sender As Object, e As EventArgs) Handles Clear.Click
    ComboBox1.ResetText()
    SystemPressure.Clear()
    LengthMeters.Clear()
    LabelVolume.Text = "Volume"
    TestTimeMinutes.Text = " : Minutes"
    TestTimeHours.Text = " : Hours"
    TestPressure.Text = "Kpa"


End Sub

我建议您需要进行一些防御性的编程。

我建议您在尝试处理输入值之前先对其进行检查。 另外, Select Case将更有意义,以捕获意外的值(这可能会导致您找到错误)

像这样:

Dim kpa As Double

If Double.TryParse(SystemPressure.Text, kpa) Then
    Select Case kpa
        Case 0 To 7
            TestPressure.Text = kpa.ToString
        Case Is > 7.1
           TestPressure.Text = (1.5 * kpa).ToString
        Case Else
            Throw New Exception(String.Format("The input value {0} was an invalid value", kpa))
    End Select
Else
    Throw New Exception(String.Format("The input value {0} is not a numeric value", kpa))
End If

另外,您应该将Option Strict On这将长期帮助您

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM