如果跑步者在健身追踪应用程序中保持步伐,我如何保持追踪?

How can I keep track if runner is on pace in fitness tracking app?

我正在经历 Apple "App Development With Swift" iBook 中的挑战,并且在完成第 2.2 课 - 功能中的健身应用程序时遇到了障碍。我想不出一个好的公式来跟踪用户是否跟上节奏。我仍然是一个菜鸟,这是迄今为止我想出的,显然不能准确地跟踪节奏。

func pacing(currentDistance: Double, totalDistance: Double, currentTime: Double, goalTime: Double) {

        if (currentDistance < 0.50 * totalDistance && currentTime > 0.40 * goalTime)     {
            print("You've got to push it just a bit harder!")
    }
    else {
            print("Keep it up!")
    }
}
pacing(currentDistance: 1, totalDistance: 10, currentTime: 8, goalTime:60)

书中的挑战告诉你要做到以下几点: 您的健身追踪应用程序将帮助跑步者保持步伐以达到他们的目标。编写一个名为 pacing 的函数,该函数采用四个 Double 参数,分别为 currentDistance、totalDistance、currentTime 和 goalTime。您的函数应该计算用户是否正在按速度达到或超过 goalTime。如果是,打印"Keep it up!",否则打印"You've got to push it just a bit harder!"

我们知道 Distance = Speed * Time ,所以在这里你想知道当前的速度是多少,并根据它打印相应的消息所以你可以尝试这样的事情:

func pacing(currentDistance: Double, totalDistance: Double, currentTime: Double, goalTime: Double) {

        let goalSpeed = totalDistance / goalTime
        let currentSpeed = currentDistance / currentTime

        if (currentSpeed < goalSpeed)     {
                print("You've got to push it just a bit harder!")
        }
        else {
                print("Keep it up!")
        }
}
pacing(currentDistance: 1, totalDistance: 10, currentTime: 8, goalTime:60)