My function is not working as it stated in question

1 回表示 (過去 30 日間)
Sashi Kumar
Sashi Kumar 2017 年 12 月 29 日
編集済み: Sashi Kumar 2017 年 12 月 29 日
This is my question:
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. The code I done is below
function total = fare(miles, age)
x =2;
if round(miles) <=1
s = x;
elseif round(miles) < 10
s = x + ((round(miles)-1)*0.25);
else
s = x + 9 * 0.25 + 0.10*(round(miles)-10);
end
if age <=18 || age >=60
total = s - (s*0.2);
else
total = s;
end
fare(1 ,16)
ans = 2
I am getting wrong answer for this code. Somehow the if else statement in age is not functioning. I like to what mistake I done here

採用された回答

Walter Roberson
Walter Roberson 2017 年 12 月 29 日
You have
if age <=18 && age >=60
There are no numbers that are simultaneously less than 18 and greater than 60.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by