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 日間)
DJ V
DJ V 2016 年 11 月 26 日
閉鎖済み: Stephen23 2016 年 11 月 27 日

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
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
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.

DJ V
DJ V 2016 年 11 月 26 日

payment = fare(1.45,17)

pay2 =

   11.2500

payment =

    1.6900

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by