Robię mały projekt z vb.net i mam problem z przywróceniem kosztów ogrzewania do wartości nieprzemiennej.
Wywołaj funkcję, aby obliczyć, czy pula jestmały, średni lub duży. Użyj odpowiedniej struktury rozgałęziającej w funkcji i zwróć String określając kategorię puli. Program obsługi zdarzeń przypisze ten ciąg do pliku Właściwość Text etykiety kategorii. Teraz z tym tutaj mam etykietę o nazwie lblCategory.
Na koniec wstaw strukturę pętli do zdarzeniaobsługi do obliczenia wartości ogrzewania w dolarach na miesiąc. Potrzebujesz pętli do zmiany zmiennej temperatury (która będzie musiała być typem zmiennoprzecinkowym) od wartości od 5 do 23 cali kroki co 1,5.
Wewnątrz tej pętli dodaj wartość zmiennej temperatury do pola tekstowego Średnia temp i dodaj odpowiednią wartość do pola tekstowego $ Per Month, wywołując funkcję, która używa następującej formuły:
Koszt ogrzewania = (25 - temperatura) * objętość / 32500
To obliczy koszt ogrzewaniabasen do 25 stopni, mnożąc różnica temperatur w zależności od objętości basenu i regulacja o wcześniej określoną wartość. Upewnij się, że funkcja zwraca nie zmiennoprzecinkowy typ danych, aby usunąć wszystkie wartości zmiennoprzecinkowe z wyniku.
To jest mój kod do tej pory ...
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
Każda udzielona pomoc byłaby dla mnie bardzo miła i pomocna, aby dokończyć tę rzecz, dziękuję za przejrzenie i poświęcenie czasu na jej przejście
Odpowiedzi:
0 dla odpowiedzi № 1Masz jedno wyjście Sub za dużo w swoim kodzie. Po twoich trzech blokach warunków pojawi się Exit Sub, które zawsze będzie wychodzić z twojego subwoofera w tej linii.
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
Usuń ostatnie „Exit Sub” ^^
0 dla odpowiedzi nr 2
Twoje funkcje ValidXXX () wymagają trochę pracy.Jeśli użytkownik wpisze na przykład „ChokeOnThis” w TextBox, program ulegnie awarii i wypali. Dzieje się tak, ponieważ przekazujesz String, w którym oczekuje się Single. Powoduje to niejawną (automatyczną) konwersję z String na Single. Jeśli String nie jest prawidłową Single (jak w przykładzie „ChokeOnThis”), zgłaszany jest wyjątek i program ulega awarii.
Właściwym sposobem na to byłoby otrzymanie plikuCiąg znaków do funkcji ValidXXX (), a następnie wewnętrznie użyj funkcji Integer.TryParse () w ten sposób. Ponownie zwróć uwagę, że parametr „TestLength” funkcji to ciąg:
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
Wprowadzisz podobne zmiany w innych funkcjach ValidXXX ().
Ten sam problem występuje w przypadku urządzenia SurfaceArea ()i inne funkcje obliczeniowe, ponieważ oczekują parametrów Integer, ale przekazujesz im wartości String. Ponieważ łańcuchy znaków zostaną sprawdzone w tym momencie, program nie zawiesił się. To po prostu zły projekt, aby te konwersje następowały wielokrotnie. Lepszy projekt sprawdziłby dane wejściowe i miałby przekonwertowane wartości całkowite przechowywane w zmiennych lokalnych, aby można je było przekazać do funkcji obliczeniowych.