Help with if, else if & else statements
이전 댓글 표시
I am working on the following question for a class. Write a function called fare that computes the bus fare one must pay in a given city based on the distance travelled. Here is how the fare is calculated: the first mile is $2. Each additional mile up to a total trip distance of 10 miles is 25 cents. Each additional mile over 10 miles is 10 cents. Miles are rounded to the nearest integer other than the first mile which must be paid in full once a journey begins. Children 18 or younger and seniors 60 or older get a 20% discount. The inputs to the function are the distance of the journey and the age of the passenger in this order. Return the fare in dollars, e.g., 2.75 would be the result returned for a 4-mile trip with no discount.
So far I have written the following but when I run the debugger I keep getting messages about my ends not being in the correct location. I have fiddled with moving the code around with the if, else and else if statements, but I seem to be missing something.
function f = fare(dm,ay)
dm = round(dm);
if (dm <= 1)
f = 2;
else if (dm> 1 && dm >= 10)
f = 2 + (dm-1) * 0.25;
else if (m > 10)
f = 2 + ((dm-1) * 0.25) + ((dm-10)* 0.10);
if (ay <= 18 || ay >= 60)
p = f(.80);
else
p = f;
end
채택된 답변
추가 답변 (1개)
Image Analyst
2019년 1월 12일
0 개 추천
Ah, our old friend - the bus fare homework question.
Have you checked out all the other answers for this? Try that first before we go over it yet again.
Click for The Bus Fare Homework Problem
카테고리
도움말 센터 및 File Exchange에서 Just for fun에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!