Trying to calculate tax due based on varying reported taxable incomes. All input values return my if statement condition ($0).

1 回表示 (過去 30 日間)
t=input('Enter total income: ');
% Calculate Tax due
if (0<t<=6000)
due= 0
elseif (6000<t<=34000)
due= 0.15*(t-6000)
elseif (34000<t<=80000)
due= 4200+0.30*(t-34000)
elseif (80000<t<=180000)
due= 18000+0.40*(t-80000)
elseif (180000<t)
due= 58000+0.45*(t-180000)
else
disp('invalid input')
end

採用された回答

Walter Roberson
Walter Roberson 2018 年 3 月 29 日
80000<t<=180000 means ((80000<t)<=180000) . The first part, (80000<t) returns 0 (false) or 1 (true), and the second part compares that 0 or 1 to 180000, which is always satisfied.
  2 件のコメント
Steven Lord
Steven Lord 2018 年 3 月 30 日
And in fact if you write your code in MATLAB Editor in release R2017b or later (at least that's as far back as I checked) Code Analyzer will warn you that (a < x < b) and similar for the other relational operators may not do what you think it does and offer guidance on what you should do instead.
(80000 < t) & (t <= 180000)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by