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

1 visualización (últimos 30 días)
DJ V
DJ V el 26 de Nov. de 2016
Editada: Stephen23 el 26 de Nov. de 2016
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?

Respuestas (1)

Stephen23
Stephen23 el 26 de Nov. de 2016
Editada: Stephen23 el 26 de Nov. de 2016
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

Categorías

Más información sobre Quantum Mechanics en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by