Error on Beginner Code (Tax Calculation)
8 ビュー (過去 30 日間)
古いコメントを表示
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 is’ num2str(t))
3 件のコメント
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
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!