2017-07-27 23 views
0

我是一個嘗試通過蘋果的「應用程序開發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) 

我不明白我該如何捕獲返回值,實驗室的最後幾句話讓我困惑。 「如果用戶在快速返回」保持它!「,並返回」你必須更加努力!「,否則調用函數並打印返回值。」這不是那兩個不同的印刷品,那麼我該如何返還這兩種印刷品,而其他部分又是什麼意思?

回答

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的查找當前的速度,然後我發現的時候,或者你可以說etimated如果我繼續按照當前速度完成總距離,那麼我從calculatePace函數返回此時間,然後將此時間與目標時間進行比較,如果我花費更多時間來完成總距離,那麼我會必須加倍努力,否則就繼續下去。希望能幫助到你。

+0

再次感謝你,唯一我不明白的是爲什麼你在調度函數中爲currentDistance和totalDistance使用currentDistance? – SpydreX

+0

我更新了我的答案,請檢查解釋。 – 3stud1ant3

0

這裏是我的方法使用更多的步驟,而不太簡潔的像我這樣的noobs的邏輯路徑:使用第一個函數打印估計完成(第一作業要求)&返回剩餘時間(第二個作業要求),然後第二個函數向亞軍發送消息(也是第二作業要求):

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) 
相關問題