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

2 vues (au cours des 30 derniers jours)
DJ V
DJ V le 26 Nov 2016
Modifié(e) : Stephen23 le 26 Nov 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?

Réponses (1)

Stephen23
Stephen23 le 26 Nov 2016
Modifié(e) : Stephen23 le 26 Nov 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

Catégories

En savoir plus sur Quantum Mechanics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by