2017-07-27 2 views
0

私は、アップルの「App Development With Swift」iBookで素早く勉強しようとしているので、レッスン2.2の最終ラボを完成させることができません。誰でも研究室を完成させる正しい方法で私を導くことができますか? ここではラボが求めていることがあります:次に、currentDistance、totalDistance、currentTime、およびgoalTimeという4つのDoubles引数を取るpacingという関数を記述します。この関数はStringを返します。Stringはユーザーを示すメッセージです。この関数は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) 

私は戻り値を取得するはずですが、ラボの最後の数文は私を混乱させます。ユーザーがペースを上回っている場合は、「それを維持してください!」を返し、「それはちょっと強く押す必要があります」、そうでなければ関数を呼び出して戻り値を出力します。それらの2つの異なるプリントではないので、両方を返すにはどうすればいいのでしょうか?

答えて

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) 

説明:私は、私は時間を見つけたり、etimated言うことができ、現在の速度を見つけるために、currentDistanceとCURRENTTIMEを使用するすべての ファースト私が現在のスピードを上げ続けるとトータル距離を完了するのにかかる時間、calculatePace関数からこの時間を返して、次に目標時間とこの時間を比較しました。そうしなければもっと追いつくだけです。それが役に立てば幸い。

+0

はありがとうございましたあなたはペーシング機能にcurrentDistanceとtotalDistanceの両方のためのcurrentDistanceを使用しなかった理由、私は理解していない唯一のものはありますか? – SpydreX

+0

私の答えを更新しました。説明を確認してください。 – 3stud1ant3

0

私のようなnoobsの論理パスをあまり簡潔にしないで、より多くのステップを使用して私のアプローチです:最初の機能を使用して推定終了(第1割り当て要件)&残りの時間(2番目の割り当て要件)メッセージにランナー(また、第二の割当要件):

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) 
関連する問題