如何解决本课程2.2使用Swift iBook进行Apple应用程序开发的实验室?

时间:2017-07-27 05:51:19

标签: ios xcode macos swift3 swift-playground

我是一个努力学习快速学习Apple的“应用程序开发与Swift”iBook并且似乎无法完成第2.2课功能的最终实验室的菜鸟。任何人都可以用正确的方式指导我完成实验吗? 以下是实验室的要求:现在编写一个名为pacing的函数,它接受四个Doubles参数,称为currentDistance,totalDistance,currentTime和goalTime。该函数还应该返回一个String,它将是显示用户的消息。该函数应调用calculatePace,传入适当的值,并捕获返回值。然后,该函数应将返回值与goalTime进行比较,如果用户正在按步骤返回“保持原状!”,则返回“你必须更加努力地推动它!”除此以外。调用函数并打印返回值。

这是我之前在实验室不同部分的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)

我不明白我应该如何捕获返回值,实验室的最后几句话让我感到困惑。 “并且如果用户正在进行速度返回”继续保持!“并返回”你必须更加努力地推动它!“否则。调用该函数并打印返回值。”不是那两种不同的印刷品,那么我如何返回两者以及其他部分的意思呢?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

这是我的方法,使用更多步骤,而不像我这样简洁的逻辑路径:使用第一个功能打印估计完成(第一个分配要求)&返回剩余时间(第二个分配要求),然后第二个函数向跑步者发送消息(也是第二个分配要求):

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)