我正在用 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
提供的任何帮助都会非常好,对我完成这件事很有帮助,感谢您浏览并花时间完成它