I'm getting two answer from my function, I only want one. How do I fix this?
1 回表示 (過去 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?
0 件のコメント
回答 (1 件)
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
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!