Info

この質問は閉じられています。 編集または回答するには再度開いてください。

My code is producing two answers, but it is only supposed to print one.

1 回表示 (過去 30 日間)
DJ V
DJ V 2016 年 11 月 26 日
閉鎖済み: Stephen23 2016 年 11 月 27 日

my code is producing:

payment = fare(1.45,17)

pay2 =

   11.2500

payment =

    1.6900

The code itself is:

function payment = fare(miles,age)
    pay1 =0;
    pay2=0;
    pay3=0;
    if miles <= 1
        pay1 = 200;
        payment = pay1;
    elseif miles <=10
        pay1= 200;
        pay2 = (miles-1)*0.25*100
        payment = pay1+pay2;
    elseif miles>10
        pay1 = 200
        pay2 = (10-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

Why is this spitting out two answers when I only want it to return "payment"?

  2 件のコメント
John D'Errico
John D'Errico 2016 年 11 月 27 日
編集済み: John D'Errico 2016 年 11 月 27 日
Please stop asking the same question multiple times.
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

回答 (1 件)

Mischa Kim
Mischa Kim 2016 年 11 月 27 日
DJ, the function is only returning only one variable, payment.
pay2 is only displayed because there is no semi-colon after the command
pay2 = (miles-1)*0.25*100

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

Community Treasure Hunt

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

Start Hunting!

Translated by