2013-10-30 113 views
1

嗨,大家好我有一些代碼可以創建一個消息框,當用戶更改一個跟蹤欄(滑塊控件)時將出現一個消息框。它會做一些計算,然後它會檢查用戶是否通過或失敗的應用程序。問題是我的邏輯沒有考慮到水在24小時後超過100時用戶收到一個消息框,告訴他們他們已經過去了。但是,當我的開始金額爲500時,應用程序會自動假定用戶已通過並且不會提供消息。我已經評論了第二條陳述,因爲如果我沒有對它進行評論,它就從那裏開始。謝謝你的幫助!Visual Studio 2012 MessageBox.Show邏輯錯誤

這裏是我的代碼:

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 

回答

2

的問題是,我的邏輯沒有考慮當用水量超過100的用戶24小時後得到一個消息框,告訴他們已經過去了。

不應該在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 
+0

感謝您查看我的代碼。我現在覺得自己像個白癡,但這只是學習的一部分。我知道我已經正確地編碼了,我現在不在哪裏定位最後一個消息框。但是這個竅門謝謝! @unlimit –