2013-05-12 46 views
1

我正在爲班級創建微波爐應用程序。我有大部分的應用程序工作得很好,唯一的問題是我得到一個奇怪的顯示輸出,我相信它必須處理我的子字符串格式,但我不完全確定。基本上發生的事情是,如果用戶輸入1:25烹飪時間,則輸出讀數爲1:125,如果開始被擊中,則微波僅從1:00開始倒數​​。任何幫助將不勝感激!!VB微波爐錯誤顯示

Private Sub DisplayTime() 
    Dim hour As Integer 
    Dim second As Integer 
    Dim minute As Integer 

    Dim display As String ' String displays current input 

    ' if too much input entered 
    If timeIs.Length > 5 Then 
     timeIs = timeIs.Substring(0, 5) 
    End If 

    display = timeIs.PadLeft(5, "0"c) 

    ' extract seconds, minutes, and hours 
    second = Convert.ToInt32(display.Substring(2)) 
    minute = Convert.ToInt32(display.Substring(1, 2)) 
    hour = Convert.ToInt32(display.Substring(0, 1)) 

    ' display number of hours, minutes, ":" seconds 
    displayLabel.Text = String.Format("{0:D1}:{1:D2}:{2:D2}", 
    hour, minute, second) 
End Sub ' DisplayTime 

' event handler displays new time each second 
Private Sub clockTimer_Tick(sender As System.Object, 
    e As System.EventArgs) Handles clockTimer.Tick 

    ' perform countdown, subtract one second 
    If timeObject.Second > 0 Then 
    timeObject.Second -= 1 
     displayLabel.Text = String.Format("{0:D1}:{1:D2}:{2:D2}", 
     timeObject.Hour, timeObject.Minute, timeObject.Second) 
    ElseIf timeObject.Minute > 0 Then 
    timeObject.Minute -= 1 
    timeObject.Second = 59 
     displayLabel.Text = String.Format("{0:D1}:{1:D2}:{2:D2}", 
     timeObject.Hour, timeObject.Minute, timeObject.Second) 

    ElseIf timeObject.Hour > 0 Then 
     timeObject.Hour -= 1 
     timeObject.Minute = 59 
     timeObject.Second = 59 
     displayLabel.Text = String.Format("{0:D1}:{1:D2}:{2:D2}", 
     timeObject.Hour, timeObject.Minute, timeObject.Second) 
    Else ' countdown finished 
     clockTimer.Enabled = False ' stop timer 
     Beep() 
     displayLabel.Text = "Done!" ' inform user time is finished 
     windowPanel.BackColor = Control.DefaultBackColor 
    End If 
End Sub ' clockTimer_Tick 
End Class ' MicrowaveOvenForm 
+0

如果'timeIs'中有「99999」呢?這會傳遞你的Convert.ToInt32()調用,但是這應該被認爲是有效的時間? – 2013-05-12 20:11:52

回答

3

您SUBSTRING()用於提取秒部分是錯誤的。

變化:

second = Convert.ToInt32(display.Substring(2)) 

要:

second = Convert.ToInt32(display.Substring(3, 2)) 

*您必須使用 「timeObject」 有關係嗎?有更好的方法來保持倒計時...

+0

是的,我們需要我們的教師使用它。並感謝您的修復,一切正在工作! :) – 2013-05-12 21:10:26

+0

太棒了。點擊複選標記,讓其他人知道問題已解決。 – 2013-05-12 21:25:45