2

我正在为班级创建一个微波炉应用程序。我的应用程序大部分都运行良好,唯一的问题是我得到了一个奇怪的显示输出,我相信这与我的子字符串格式有关,但我不完全确定。基本上发生的事情是,如果用户输入 1:25 烹饪时间,输出读数为 1:125,如果启动,微波炉仅从 1:00 开始倒计时。任何帮助将不胜感激!!

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
4

1 回答 1

3

您用于提取秒部分的 Substring() 是错误的。

改变:

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

至:

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

*您是否需要使用“timeObject”?有更好的方法来保持倒计时......

于 2013-05-12T19:58:51.003 回答