2013-05-12 49 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