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!