Error on Beginner Code (Tax Calculation)
2 次查看(过去 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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!