0

我正在用 vb.net 做一个小项目,但我无法将加热成本返回到非浮点数。

  • 调用函数来计算池是小、中还是大。在函数中使用适当的分支结构,并返回一个说明池类别的字符串。事件处理程序将此字符串分配给类别标签的文本属性。现在有了这个,我有一个名为 lblCategory 的标签。

  • 最后,在事件处理程序中插入一个循环结构来计算每月供暖的美元价值。需要一个循环来以 1.5 的步长将温度变量(需要为浮点类型)从 5 的值更改为 23。

    在此循环中,将温度变量的值添加到 Avg Temp 文本框,并将相应的值添加到 $ Per Month 文本框,调用使用以下公式的函数:

    加热成本 = (25 – 温度) * 体积 / 32500

    这将计算将水池加热到 25 度的成本,方法是将温差乘以水池的体积,并按预定值进行调整。确保函数返回非浮点数据类型,以便从结果中删除任何浮点值。

到目前为止,这是我的代码...

Public Class Form1

Const MinLength As Integer = 5
Const MaxLength As Integer = 50
Const MinWidth As Integer = 2
Const MaxWidth As Integer = 20
Const MinDepth As Integer = 2
Const MaxDepth As Integer = 4


Private Function ValidLength(ByVal TestLength As String) As Boolean
    'Length of the pool
    Dim i As Double
    Dim Message As String = ""
    If Double.TryParse(TestLength, i) Then
        If i >= MinLength AndAlso i <= MaxLength Then
            Return True
        End If
    End If
    Message = "Length measurement is not valid." & vbCrLf & vbCrLf & "Please enter a value between 5 and 50"
    MessageBox.Show(Message, "Data Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Return False
    End
End Function

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Dim Volume As Double
    Dim Temp As Double
    Dim SA As Double
    Dim HeatingCostInt As Integer
    If Not ValidLength(txtLength.Text) Then
        Exit Sub
    End If
    If Not ValidWidth(txtWidth.Text) Then
        Exit Sub
    End If
    If Not ValidDepth(txtAvgDepth.Text) Then
        Exit Sub
    End If
    'SurfaceArea Function
    SA = Val(SurfaceArea(txtLength.Text, txtWidth.Text))
    txtSurfaceArea.Text = SA
    'Volume Function
    Volume = Val(Vol(txtLength.Text, txtWidth.Text, txtAvgDepth.Text * 1000))
    txtVolume.Text = Volume
    If PoolVolume(Volume) Then
    End If
    'Temperature Function
    For Temp = 5 To 23 Step 1.5
        txtTableAvgTemp.AppendText(Temp & Environment.NewLine)
        HeatingCost(Temp, Volume)
    Next
End Sub

Private Function ValidWidth(ByVal TestWidth As String) As Boolean
    'Width of the Pool
    Dim Message As String = ""
    Dim i As Double
    If Double.TryParse(TestWidth, i) Then
        If i >= MinWidth And i <= MaxWidth Then
            Return True
        End If
    End If
    Message = "Width measurement is not valid." & vbCrLf & vbCrLf & "Please enter a value between 2 and 20"
    MessageBox.Show(Message, "Data Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Return False
    End
End Function

Private Function ValidDepth(ByVal TestDepth As String) As Boolean
    'Depth of the pool
    Dim Message As String = ""
    Dim i As Double
    If Double.TryParse(TestDepth, i) Then
        If i >= MinDepth And i <= MaxDepth Then
            Return True
        End If
    End If
    Message = "Depth measurement is not valid." & vbCrLf & vbCrLf & "Please enter a value between 2 and 4"
    MessageBox.Show(Message, "Data Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Return False
    End
End Function

Private Function SurfaceArea(ByVal Value1 As Double, ByVal Value2 As Double) As Double
    'Calculation for SurfaceArea
    SurfaceArea = Value1 * Value2
    Return SurfaceArea
End Function

Private Function Vol(ByVal Value1 As Double, ByVal Value2 As Double, ByVal Value3 As Double) As Double
    'Calculation for Volume
    Vol = Value1 * Value2 * Value3 * 1000
    Return Vol
End Function

Private Function PoolVolume(ByVal Value1 As Double) As Boolean
    'Pool Volume size that isn't working correctly and I don't know why
    If (Value1 <= 500000) Then
        lblCategory.Text = "Pool Category: Small"
    ElseIf (Value1 > 500000 <= 1500000) Then
        lblCategory.Text = "Pool Category: Medium"
    Else
        lblCategory.Text = "Pool Category: Large"
        Value1 = lblCategory.Text
    End If
    Return PoolVolume
End Function

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    'Ends the program
    End
End Sub

Private Sub txtSurfaceArea_TextChanged(sender As Object, e As EventArgs) Handles txtSurfaceArea.TextChanged
    'Changes text of SurfaceArea
    txtSurfaceArea.Text = txtLength.Text * txtWidth.Text
End Sub

Private Sub txtVolume_TextChanged(sender As Object, e As EventArgs) Handles txtVolume.TextChanged
    'Changes text of Volume
    txtVolume.Text = txtLength.Text * txtWidth.Text * txtAvgDepth.Text * 1000
End Sub

Private Function HeatingCost(ByVal Value1 As Double, ByVal Value2 As Double) As Double
    'Calculation for Heating Cost that isn't returning as a non-floating point. Main issue right here!!! 
    Dim Value3 As Double = 32500
    HeatingCost = (25 - Value1) * Value2 / Value3
    txtTableDollars.AppendText(HeatingCost & Environment.NewLine)
End Function

End Class

提供的任何帮助都会非常好,对我完成这件事很有帮助,感谢您浏览并花时间完成它

4

2 回答 2

0

您的代码中有一个 Exit Sub。在您的三个条件块之后,有一个 Exit Sub,它将始终在此行上退出您的 sub。

If Not ValidLength(txtLength.Text) Then
    Exit Sub
End If
If Not ValidWidth(txtWidth.Text) Then
    Exit Sub
End If
If Not ValidDepth(txtAvgDepth.Text) Then
    Exit Sub
End If
Exit Sub

删除最后一个“退出子”^^

于 2013-10-23T11:08:21.877 回答
0

您的 ValidXXX() 函数需要一些工作。例如,如果用户在 TextBox 中键入“ChokeOnThis”,程序将崩溃并烧毁。这是因为您正在传递一个 String ,其中需要一个 Single 。这会导致从 String 到 Single 的隐式(自动)转换。如果 String 不是有效的 Single(如“ChokeOnThis”示例),则会引发异常并且程序崩溃。

执行此操作的正确方法是将 String 接收到 ValidXXX() 函数中,然后像这样在内部使用 Integer.TryParse() 函数。再次注意,函数的参数“TestLength”是一个字符串:

Private Function ValidLength(ByVal TestLength As String) As Boolean
    'Length of the pool
    Dim i As Single
    If Single.TryParse(TestLength, i) Then
        If i >= MinLength AndAlso i <= MaxLength Then
            Return True
        End If
    End If

    Dim Message As String = _
        "Length measurement is not valid." & vbCrLf & vbCrLf & _
        "Please enter a value between 5 and 50"
    MessageBox.Show(Message, "Data Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Return False
End Function

您将对其他 ValidXXX() 函数进行类似的更改。

您的 SurfaceArea() 和其他计算函数也存在同样的问题,因为它们需要 Integer 参数,但您正在向它们传递 String 值。但是,由于此时字符串已被验证,因此程序不会崩溃。多次发生这些转换只是一个糟糕的设计。更好的设计将验证输入并将转换后的整数值存储在局部变量中,以便将它们传递给计算函数。

于 2013-10-23T14:30:02.643 回答