Help with if, else if & else statements

조회 수: 6 (최근 30일)
Hilary Gaiser
Hilary Gaiser 2019년 1월 12일
답변: Walter Roberson 2019년 1월 12일
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

채택된 답변

Walter Roberson
Walter Roberson 2019년 1월 12일
You are using else if as if it was elseif
That is,
if condition1
action1
else if condition2
action2
end
is not valid. Valid would be
if condition1
action1
else if condition2
action2
end
end
which is equivalent to
if condition1
action1
else
if condition2
action2
end
end
Also legal is
if condition1
action1
elseif condition2
action2
end
Notice there is only a single end
You are probably wondering what the difference is between them. Well suppose you had
r = rand;
if r <= 1/2
disp('first half')
else if r <= 3/4
disp('third quarter')
end
disp('another message')
end
Then when we space it better, we get
r = rand;
if r <= 1/2
disp('first half')
else
if r <= 3/4
disp('third quarter')
end
disp('another message')
end
and you can see that 'another message' sent out as long as r <= 1/2 is false, including if r <= 3/4 is ture. But if we had
r = rand;
if r <= 1/2
disp('first half')
elseif r <= 3/4
disp('third quarter')
end
disp('another message')
then "another message" is always sent out.

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 1월 12일
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.
  댓글 수: 1
Hilary Gaiser
Hilary Gaiser 2019년 1월 12일
Oh, I have spent the whole evening looking over similar questions but I can’t quite seem to grasp it. I would like to have a better understanding of what I am doing wrong rather than just have someone help me fix my code.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by