我是一個嘗試通過蘋果的「應用程序開發Swift」iBook快速學習,並且似乎無法完成課程2.2功能的最終實驗的小老鼠。任何人都可以用正確的方式指導我完成實驗嗎? 以下是實驗室要求的內容:現在編寫一個稱爲步調的函數,它帶有四個雙重參數,分別爲currentDistance,totalDistance,currentTime和goalTime。該函數還應該返回一個字符串,這將是顯示用戶的消息。該函數應該調用calculatePace,傳遞適當的值並捕獲返回值。然後該函數應該將返回的值與goalTime進行比較,如果用戶正在加速返回「Keep it up!」,並返回「您必須更加努力!」除此以外。調用該函數並打印返回值。如何解決這個課2.2實驗室在蘋果的應用程序開發與Swift iBook?
這裏是較早在實驗室的不同部分我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)
我不明白我該如何捕獲返回值,實驗室的最後幾句話讓我困惑。 「如果用戶在快速返回」保持它!「,並返回」你必須更加努力!「,否則調用函數並打印返回值。」這不是那兩個不同的印刷品,那麼我該如何返還這兩種印刷品,而其他部分又是什麼意思?
再次感謝你,唯一我不明白的是爲什麼你在調度函數中爲currentDistance和totalDistance使用currentDistance? – SpydreX
我更新了我的答案,請檢查解釋。 – 3stud1ant3