![](/img/trans.png)
[英]How to keep track of data over various periods of time (Swift) - Counter/Habit Tracking App
[英]How can I keep track if runner is on pace in fitness tracking app?
我正在經歷蘋果公司的“使用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。 您的函數應計算用戶是否在達到或擊敗目標時間上。 如果是,請打印“保持!”,否則請打印“您必須將其推得更緊一點!”
我們知道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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.