Question

Hey everyone i have some code that creates a message box that will appear when the user changes a track bar(slider control). It will do some calculations then it will check if the user passes or fails the application. The problem is that my logic doesn't account for when the water is above 100 after 24hrs the user gets a message box that tells them they have passed. But when i having a starting amount of 500 the application automatically assumes that the user has passed and doesn't give a message. I have commented out the second if statement because it simply starts there if i didn't comment it out. Thanks for any help!

Here is my code:

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

    Dim Gallons As Double = 500 'Constant Starting amount
    Dim LeakRate As Double = 0.1  'Constant leak for 24hr
    Dim Remainder As Integer    'Creating a variable
    Dim Time As Integer = tkbSlider.Value 'Gets the Value of the Slider or Track Bar
    lstRemainingGallons.Items.Clear()  'Clears the Collection of ListBox

    Try

        For n As Integer = 1 To 24  'Setting Up the Loop 
            Remainder = CInt(Gallons * LeakRate) - Time 'Math Calculation 
            Gallons = CInt(Gallons - Remainder)  'More Math 
            If Gallons <= 99 Then       'Displaying Message

                MessageBox.Show("Fish died at " & n & " hours when the remaining gallons were " & Gallons, "Dead and Stinking Fish", MessageBoxButtons.OK
                    )
                Exit Sub
            End If

            'If Gallons > 100 Then  'Displaying Message
                'MessageBox.Show("Get out the BBQ! You made it!", "Fish Frying Time!")
            'End If
            lstRemainingGallons.Items.Add("Hour # " & n & " -  " & Gallons & "gallons")

        Next
    Catch ex As Exception   ' Catching an Execption if any?

    End Try

  End Sub
  End Class
Was it helpful?

Solution

The problem is that my logic doesn't account for when the water is above 100 after 24hrs the user gets a message box that tells them they have passed.

Shouldn't you show the message after the for loop(after 24 hours have passed)?

For n As Integer = 1 To 24  'Setting Up the Loop 
        Remainder = CInt(Gallons * LeakRate) - Time 'Math Calculation 
        Gallons = CInt(Gallons - Remainder)  'More Math 
        If Gallons <= 99 Then       'Displaying Message

            MessageBox.Show("Fish died at " & n & " hours when the remaining gallons were " & Gallons, "Dead and Stinking Fish", MessageBoxButtons.OK
                )
            Exit Sub
        End If


        lstRemainingGallons.Items.Add("Hour # " & n & " -  " & Gallons & "gallons")

    Next
If Gallons > 100 Then  'Displaying Message
     MessageBox.Show("Get out the BBQ! You made it!", "Fish Frying Time!")
End If
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top