Ios 如何在Apple';使用Swift iBook开发应用程序?
我是一个试图通过苹果的“应用程序开发与swift”iBook学习swift的noob,似乎无法完成第2.2课功能的最后一个实验。谁能指导我用正确的方法完成这个实验? 以下是实验室的要求:现在编写一个名为packing的函数,该函数接受四个双参数,分别为currentDistance、totalDistance、currentTime和goalTime。函数还应返回一个字符串,该字符串将作为向用户显示的消息。函数应该调用CalculatePath,传入适当的值,并捕获返回值。然后,函数应该将返回的值与goalTime进行比较,如果用户正在配速,则返回“跟上!”,否则返回“你必须再用力推一点!”。调用函数并打印返回值 以下是我的CalculatePase函数,它来自实验室的另一部分:Ios 如何在Apple';使用Swift iBook开发应用程序?,ios,xcode,macos,swift3,swift-playground,Ios,Xcode,Macos,Swift3,Swift Playground,我是一个试图通过苹果的“应用程序开发与swift”iBook学习swift的noob,似乎无法完成第2.2课功能的最后一个实验。谁能指导我用正确的方法完成这个实验? 以下是实验室的要求:现在编写一个名为packing的函数,该函数接受四个双参数,分别为currentDistance、totalDistance、currentTime和goalTime。函数还应返回一个字符串,该字符串将作为向用户显示的消息。函数应该调用CalculatePath,传入适当的值,并捕获返回值。然后,函数应该将返回的
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来计算当前速度,然后我计算时间,或者你可以说,如果我继续以当前速度计算,完成总距离所需的预计时间,我从CalculatePath函数返回这次,然后我将这次与目标时间进行比较,如果我需要更多的时间来完成总距离,那么我必须更加努力,否则就只能跟上。希望能有所帮助。以下是我的方法,我使用了更多的步骤,而不是像我这样的noob的逻辑路径:使用第一个函数打印预计完成时间(第一个任务要求)并返回剩余时间(第二个任务要求),然后使用第二个函数向跑步者发送消息(也是第二个任务要求):
func calculatePace(当前距离:双精度,总距离:双精度,当前时间:双精度)->Double{
让速度=当前距离/当前时间
让剩余距离=总距离-当前距离
让剩余时间=剩余距离/速度
打印(“预计完成时间:\(当前时间+剩余时间)”)
返回剩余时间
}
func起搏(当前距离:双精度,总距离:双精度,当前时间:双精度,目标时间:双精度){
如果(currentTime+CalculatePath(currentDistance:currentDistance,totalDistance:totalDistance,currentTime:currentTime))再次感谢您,我唯一不明白的是,为什么您在起搏功能中同时使用currentDistance作为currentDistance和totalDistance?
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)