【问题标题】:How can I keep track if runner is on pace in fitness tracking app?如果跑步者在健身追踪应用程序中保持同步,我该如何追踪?
【发布时间】:2017-07-27 04:12:03
【问题描述】:

我正在经历 Apple 的“使用 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。您的函数应计算用户是否按步数击中或击败目标时间。如果是,打印“坚持下去!”,否则打印“你必须再努力一点!”

【问题讨论】:

    标签: ios xcode macos swift3 swift-playground


    【解决方案1】:

    我们知道距离 = 速度 * 时间,所以在这里您想知道当前速度是多少,并基于此打印相应的消息,因此您可以尝试以下操作:

    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)
    

    【讨论】:

    • 非常感谢,非常棒的回答。我不能要求更好。
    猜你喜欢
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 2016-11-27
    相关资源
    最近更新 更多