Error on Beginner Code (Tax Calculation)

8 ビュー (過去 30 日間)
Julia O'Bryant
Julia O'Bryant 2019 年 12 月 16 日
コメント済み: Guillaume 2019 年 12 月 16 日
Hi! I am brand new to matlab and need some help with this code. I am trying to create a code that asks for the user's net income, calculates the tax, and displays the amount. There is no tax on the first $15,000 of the net income, 5% on every dollar between $15,001-25,000, and 10% tax on every dollar above $25,000. I know that using t=tax is causing the error, but I don't know how else to write the code. Any ideas on how to fix this?
Here is what I have so far:
x = input(Enter your net income: );
t = tax
if x<=15000
t = 0
elseif x>15000 && x<=25000
t = (x-15000) * 0.05
elseif x>25000
t = (x-25000) * 0.1
end
disp(Your tax isnum2str(t))
  3 件のコメント
Julia O'Bryant
Julia O'Bryant 2019 年 12 月 16 日
I am just confused because when I do not define t before my if statements, matlab says that t is an undefined variable. I do not know what to define it as or how to write the program so that t is not needed.
Guillaume
Guillaume 2019 年 12 月 16 日
You'll have to explain better the problem, since when I run this code (the same as your minus the t = tax line) I get no error (for valid values of x):
x = input('Enter your net income: ');
if x<=15000
t = 0
elseif x>15000 && x<=25000
t = (x-15000) * 0.05
elseif x>25000
t = (x-25000) * 0.1
end
You will indeed get an undefined t if none of your if tests are true (which may be the case if x is non-scalar or x is NaN

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

採用された回答

Karthick S
Karthick S 2019 年 12 月 16 日
x = input('Enter your net income:');
if x<=15000
t = 0;
elseif x>15000 && x<=25000
t = (x-15000) * 0.05;
elseif x>25000
t = ((x-15000) * 0.05)+((x-25000) * 0.1);
end
disp(sprintf('Your tax is %s', num2str(t)));

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by