如何使用 Apple 的应用程序开发 2.2 练习解决此课程 2.2 实验室?



我是一个菜鸟,试图用苹果的"App Development With Swift"iBook学习swift,似乎无法完成2.2课函数的最后实验。谁能指导我正确完成实验? 以下是实验室的要求:现在编写一个名为 paceing 的函数,该函数采用四个双精度参数,分别称为 currentDistance、totalDistance、currentTime 和 goalTime。该函数还应返回一个字符串,该字符串将是向用户显示的消息。该函数应调用 calculatePace,传入适当的值,并捕获返回值。然后,该函数应将返回的值与 goalTime 进行比较,如果用户在配速,则返回"坚持下去!",否则返回"你必须用力一点!调用该函数并打印返回值。

以下是我之前在实验室不同部分的计算速度函数:

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)

我不明白我应该如何捕获返回值,实验室的最后几句话让我感到困惑。"如果用户在跟上步伐返回"坚持下去!",然后返回"你必须用力一点!"否则。调用该函数并打印返回值。这不是两个不同的打印件吗,那么我如何返回两者,否则部分是什么意思?

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

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函数返回这个时间,然后我把这个时间与目标时间进行了比较, 如果我花更多的时间来完成总距离,那么我将不得不更加努力,否则就跟上。希望对您有所帮助。

这是我使用更多步骤的方法,而对于像我这样的菜鸟来说,逻辑路径不那么简洁: 使用第一个函数打印估计完成时间(第一个分配要求(并返回剩余时间(第二个分配要求(,然后第二个函数向运行器发送消息(也是第二个分配要求(:

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((

相关内容

最新更新