I'm getting two answer from my function, I only want one. How do I fix this?

2 次查看(过去 30 天)
function payment = fare(miles,age)
tot1 =0;
tot2=0;
tot3=0;
if miles <= 1
tot1 = 200;
payment = tot1;
elseif miles <=10
tot1= 200;
tot2 = (miles-1)*0.25*100
payment = tot1+tot2;
elseif miles>10
tot1 = 200
tot2 = (10-1)*0.25*100
tot3 = (miles-10)*0.10*100
payment = tot1+tot2+tot3;
end
payment = (tot1+tot2+tot3)/100;
if age <=18 || age>=65
payment = (tot1+tot2+tot3)*0.8/100;
else
payment=(tot1+tot2+tot3)/100;
end
end
This is the code that produces the two variables, while I only want one: payment.
How do I get it?

回答(1 个)

Stephen23
Stephen23 2016-11-26
编辑:Stephen23 2016-11-26
Your code omly returns one variable, payment, but it will print several other variables to the command window because you did not put semi-colons at the end of the lines:
elseif miles <=10
tot1= 200;
tot2 = (miles-1)*0.25*100 % <- add semicolon
payment = tot1+tot2;
elseif miles>10
tot1 = 200 % <- add semicolon
tot2 = (10-1)*0.25*100 % <- add semicolon
tot3 = (miles-10)*0.10*100 % <- add semicolon

类别

Help CenterFile Exchange 中查找有关 Quantum Mechanics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by