有问题的if-Statement

时间:2015-01-04 16:40:11

标签: c# if-statement windows-phone-8

我之前曾尝试过两三次问这个问题,但未能正确分阶段,所以我想再试一次。我正在制作一个训练秒表应用程序。该应用程序的功能是计算10秒,让受训者10秒准备。一旦该时间过去,它将计入20秒,其中受训者在20秒内进行艰苦锻炼。之后,休息时间为10秒。然后它循环回到20秒的训练并继续这个循环8轮。

我的问题是,它将开始10秒准备,但然后它循环回到10秒计时器。出于某种原因,我的if else语句会循环回到准备时间。 我使用秒表和时间跨度来表示if语句。

private void timer_Tick(object sender, EventArgs e)
    {
        //throw new NotImplementedException();
        //timer timespan is more than zero, start stopwatch(get the prepare counter going)
        if (timerWatch.Elapsed < new TimeSpan(0, 0, 11))// if its 5sec
        {
            milllisecond = timerWatch.ElapsedMilliseconds;
            second = milllisecond / 1000;
            milllisecond = milllisecond % 1000;
            minute = second / 60;
            second = second % 60;
            hour = minute / 60;
            minute = minute % 60;
            txtblTime.Text = minute.ToString("00") + ":" + second.ToString("00");
        }
        else if (timerWatch.Elapsed >= new TimeSpan(0, 0, 10) && timerWatch.Elapsed < new TimeSpan(0, 0, 21))//more than 4sec
        {
            timerWatch.Restart();

            milllisecond = timerWatch.ElapsedMilliseconds;
            second = milllisecond / 1000;
            milllisecond = milllisecond % 1000;
            minute = second / 60;
            second = second % 60;
            hour = minute / 60;
            minute = minute % 60;
            txtblTime.Text = minute.ToString("00") + ":" + second.ToString("00");

            txtblPrepare.Visibility = System.Windows.Visibility.Collapsed;
            txtblGo.Visibility = System.Windows.Visibility.Visible;
        }
        else if (timerWatch.Elapsed < new TimeSpan(0, 0, 21))
        {
            timerWatch.Restart();

            milllisecond = timerWatch.ElapsedMilliseconds;
            second = milllisecond / 1000;
            milllisecond = milllisecond % 1000;
            minute = second / 60;
            second = second % 60;
            hour = minute / 60;
            minute = minute % 60;

            txtblTime.Text = minute.ToString("00") + ":" + second.ToString("00");
            txtblGo.Visibility = System.Windows.Visibility.Collapsed;
        }
        else
            txtblTime.Text = "Times Up!";                    
    }

3 个答案:

答案 0 :(得分:5)

您要实施的是所谓的finite-state machine。通过计时器推断该机器的状态使得难以理解和读取代码。我会以更直接的方式存储州。正如Plutonix所说,使用枚举

public enum TrainingState
{
    Stopped, // The state machine is not working yet or has finished its work.
    Preparing,
    Working,
    Resting
}

此外,您需要一个循环次数的计数器

private TrainingState _state = TrainingState.Stopped;
private int _roundNo;

现在你可以像这样制定状态转换

private void ChangeState()
{
    switch (_state) {
        case  TrainingState.Stopped:
            //TODO: Initialize and start the timer, display state
            _state = TrainingState.Preparing;
            break;
        case  TrainingState.Preparing:
            //TODO: Adjust timer intervall for working phase, display state
            _roundNo = 1; // We are going into the first round
           _state = TrainingState.Working;
            break;
        case  TrainingState.Working:
             //TODO: Adjust timer intervall for resting phase, display state
            _state = TrainingState.Resting;
            break;
        case  TrainingState.Resting:
            if (_roundNo == 8) {
                _state = TrainingState.Stopped;
                //TODO: stop timer, display state
            } else {
                //TODO: Adjust timer intervall for working phase, display state
                _roundNo++; // We are going into the next round
               _state = TrainingState.Working;
            }
            break;
    }
}

timer_Tick开始和内部时,您必须调用此方法。

private void timer_Tick(object sender, EventArgs e)
{
    ChangeState();
}

private void btnStart_Click(object sender, EventArgs e)
{
    ChangeState();
}

答案 1 :(得分:3)

以下是使用Enum跟踪阶段的基本要素。这似乎比跟踪秒表和TimeSpans更容易......当计时器熄灭时,情况会发生变化。

private enum Phases
{
    Stopped,
    Prep,
    WorkOut,
    Rest
}

private Phases thisPhase;       // phase tracker
private int workOutCount;       // 8 cycle counter

private void Timer1_Tick(object sender, EventArgs e)
{
    Timer1.Enabled = false;

    // ToDo: Fiddle with controls as needed
    // also the durations...not sure if a new Prep
    // follows the final rest or if they are the same
    switch (thisPhase) {
        case Phases.Stopped:
            // start things off
            thisPhase = Phases.Prep;
            Timer1.Interval = 10000;
            // prep time
            break;

        case Phases.Prep:
            workOutCount = 1;
            thisPhase = Phases.WorkOut;
            Timer1.Interval = 20000;
            // work out time
            break;

        case Phases.WorkOut:
            thisPhase = Phases.Rest;
            Timer1.Interval = 10000;
            // rest time
            break;

        case Phases.Rest:
            workOutCount += 1;

            if (workOutCount == 8) {
                thisPhase = Phases.Prep;
                // perhaps to None, if there is an instruction timeout
                Timer1.Interval = 10000;
                // prep time
                // actually means 10sec rest + 10 prep before next workout task
            } else {
                // next workout starts in...
                Timer1.Interval = 10000;
                // prep time
            }

            break;
    }
    Timer1.Enabled = true;
}

从VB转换可能不完美,但它应该传达这个想法。

答案 2 :(得分:0)

您正在使用

timer.Restart()

重置计时器,因此它再次执行第一种情况。查看Stopwatch的MSDN文档。当然,这个答案假定您使用秒表类作为计时器。

我认为,它会至少执行一次这样的情况,以便重置计时器。