Info
此问题已关闭。 请重新打开它进行编辑或回答。
For some strange reason MATLAB spits out two answers to the following code , pay2 =, and p =. It is ony supposed to produce 1 answer
1 次查看(过去 30 天)
显示 更早的评论
The following code is spitting out two answers, and it's only supposed to produce one answer, payment.
Instead it spits out a value for pay2 first as well as one for payment. I only want an answer for payment.
Problem 2 (fare): Testing with argument(s) 4, 44 Feedback: Your function performed correctly for argument(s) 4, 44 Testing with argument(s) 1, 20 Feedback: Your function performed correctly for argument(s) 1, 20 Testing with argument(s) 1, 11 Feedback: Your function performed correctly for argument(s) 1, 11 Testing with argument(s) 0.2, 67 Feedback: Your function performed correctly for argument(s) 0.2, 67 Testing with argument(s) 0.9, 55 Feedback: Your function performed correctly for argument(s) 0.9, 55 Testing with argument(s) 1.45, 17 Feedback: Your function made an error for argument(s) 1.45, 17
Your solution is _not_ correct.
function payment = fare(miles,age) pay1 =0; pay2=0; pay3=0; if miles <= 1 pay1 = 200; payment = pay1; elseif miles <=20 pay1= 200; pay2 = (miles-1)*0.25*100 payment = pay1+pay2; elseif miles>20 pay1 = 200 pay2 = (miles-1)*0.25*100 pay3 = (miles-10)*0.10*100 payment = pay1+pay2+pay3; end payment = (pay1+pay2+pay3)/100; if age <=18 || age>=65 payment = (pay1+pay2+pay3)*0.8/100; else payment=(pay1+pay2+pay3)/100; end
end
1 个评论
Stephen23
2016-11-27
Duplicate:
https://www.mathworks.com/matlabcentral/answers/314071-i-m-getting-two-answer-from-my-function-i-only-want-one-how-do-i-fix-this
回答(2 个)
Steven Lord
2016-11-27
You have several lines inside the elseif sections of your code that do not end in semicolons. Add the semicolons at the end of those lines to suppress the display.
0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!