1

大家好,我有一些代码可以创建一个消息框,当用户更改轨迹栏(滑块控件)时会出现该消息框。它会进行一些计算,然后检查用户是否通过了应用程序。问题是我的逻辑没有考虑到 24 小时后水位高于 100 时,用户会收到一个消息框,告诉他们他们已经通过了。但是,当我的起始金额为 500 时,应用程序会自动假定用户已通过并且不发送消息。我已经注释掉了第二个 if 语句,因为如果我没有注释掉它,它就会从那里开始。谢谢你的帮助!

这是我的代码:

    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
4

1 回答 1

2

问题是我的逻辑没有考虑到 24 小时后水位高于 100 时,用户会收到一个消息框,告诉他们他们已经通过了。

您不应该在 for 循环之后显示消息(经过 24 小时后)吗?

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
于 2013-10-30T03:09:48.450 回答