如何解决 Apple 的 App Development With Swift iBook 中的第 2.2 课实验?

How to solve this lesson 2.2 lab in Apple's App Development With Swift iBook?

我是一个尝试使用 Apple 的 "App Development With Swift" iBook 学习 swift 的菜鸟,但似乎无法完成第 2.2 课“函数”中的最终实验。谁能指导我以正确的方式完成实验? 以下是实验室的要求:现在编写一个名为 pacing 的函数,该函数采用四个双精度参数,分别称为 currentDistance、totalDistance、currentTime 和 goalTime。该函数还应该 return 一个字符串,这将是向用户显示的消息。该函数应调用 calculatePace,传入适当的值,并捕获 return 值。然后,该函数应将 returned 值与 goalTime 进行比较,如果用户处于节奏 return "Keep it up!",否则 return "You've got to push it a bit harder!"。调用函数并打印 return 值。

这是我之前在实验室不同部分的 calculatePace 函数:

func calculatePace(currentDistance: Double, totalDistance: Double, currentTime: Double) -> Double {
    let currentSpeed = currentDistance / currentTime
    return ((totalDistance / currentSpeed) / 60)
}
print("\(calculatePace(currentDistance: 1, totalDistance: 10, currentTime: 6)) hours till you finish the run!")

这是我试图用来解决实验室问题但遇到问题的功能:

func pacing(currentDistance: Double, totalDistance: Double, currentTime: Double, goalTime: Double) -> String {
    //I don't know what to put in return
    return ("test return")
    calculatePace(currentDistance: 1, totalDistance: 10, currentTime: 6, goalTime: 60)
}
pacing(currentDistance: 1, totalDistance: 10, currentTime: 6, goalTime: 60)

我不明白我应该如何捕获 return 值,实验室的最后几句话让我很困惑。 "And if the user is on pace return "坚持下去!”,否则 return "You've got to push it just a bit harder!"。调用函数并打印 return 值。”这两个不同的印刷品不是吗,所以我如何 return 两者以及其他部分是什么意思?

我想这就是你想要的,请检查..

 func calculatePace(currentDistance: Double, totalDistance: Double, currentTime: Double) -> Double {
        let currentSpeed = currentDistance / currentTime
        return (totalDistance / currentSpeed)
 }
 print("\(calculatePace(currentDistance: 1, totalDistance: 10, currentTime: 6)) hours till you finish the run!")

 func pacing(currentDistance: Double, totalDistance: Double, currentTime: Double, goalTime: Double) -> String {
        let yourFinishTime = calculatePace(currentDistance: currentDistance, totalDistance: currentDistance, currentTime: currentTime)
        var message = ""

        if yourFinishTime > goalTime {
            message = "You've got to push it a bit harder!"
        }else {
            message = "Keep it up"
        }

        return message

}
pacing(currentDistance: 1, totalDistance: 10, currentTime: 6, goalTime: 60)

说明: 首先,我使用 currentDistance 和 currentTime 来找到当前速度,然后我找到时间,或者你可以说如果我继续以当前速度继续​​完成总距离所需的估计时间,我这次从 calculatePace 函数返回,然后我将这个时间与目标时间进行了比较,如果我花更多的时间来完成总距离,那么我将不得不更加努力地推动,否则就只能跟上了。希望对你有帮助。

这是我使用更多步骤的方法,但对于像我这样的新手来说逻辑路径不太简洁:使用第一个函数打印估计完成时间(第一次作业要求)和 return 剩余时间(第二次作业要求) ,然后是第二个向跑步者发送消息的函数(也是第二个分配要求):

func calculatePace(currentDistance:Double, totalDistance:Double, currentTime:Double) -> Double{
    let speed = currentDistance / currentTime
    let remainingDistance = totalDistance - currentDistance
    let remainingTime = remainingDistance / speed
    print("Estimated finish time: \(currentTime + remainingTime)")
    return remainingTime
}

func pacing(currentDistance:Double, totalDistance:Double, currentTime:Double, goalTime:Double){
    if (currentTime + calculatePace(currentDistance: currentDistance, totalDistance: totalDistance, currentTime: currentTime)) <= goalTime {
        print("Keep it up!")
    } else {
        print("You've got to push it just a bit harder!")
    }
}
pacing(currentDistance: 60, totalDistance: 240, currentTime: 15, goalTime: 60)  

我就是这样做的。

func pacing(currentDistance: Double, totalDistance: Double, currentTime: Double, goalTime: Double) -> String {
if calculatePace(currentDistance: 17.9, totalDistance: 36, currentTime: 28.37) < goalTime {
    return "You've got to push it just a bit harder!"
} else {
    return "Keep it up!"
}

}

打印(起搏(当前距离:17.9,总距离:36,当前时间:28.37,目标时间:45))