problems in switch and if commands
1 次查看(过去 30 天)
显示 更早的评论
Hi guys I am having problems in solving this question can some one help me out please ? :)
I need to use the switch and if commands.
1 个评论
Image Analyst
2013-5-10
编辑:Image Analyst
2013-5-10
Notice: Charlene edited the vast majority of her question away, probably because it was a homework question.
采纳的回答
Image Analyst
2013-5-9
Make up a 4 by 6 matrix with those numbers in it.
Then ask your user, say with inputdlg(), what their income is. By knowing that, you know what matrix row to use. You can use a switch for that.
Then use questdlg() to ask whether they are married. By knowing that you know what columns to use, 1-3, or 4-6. You can use an if statement to set the column numbers.
Finally, simply look up the required RATE and DEDUCT from the proper row and columns.
Not hard at all. Give it a shot.
0 个评论
更多回答(1 个)
Image Analyst
2013-5-9
I can do some of it but not all of it for you, because it's your homework, not mine.
rateTable = [...
8500 0 0 11900 0 0
14500 0.15 1275 21200 0.15 1785
19500 0.25 2725 28700 0.25 3905
inf 0.35 4675 inf 0.35 6775]
% Ask user for a number.
defaultValue = 20000;
titleBar = 'Enter a value';
userPrompt = 'Enter your income';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
income = str2double(cell2mat(caUserInput));
% Check for a valid integer.
if isnan(income)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
income = defaultValue;
message = sprintf('I said it had to be a number.\nI will use %d and continue.', income);
uiwait(warndlg(message));
end
message = sprintf('Are you married');
button = questdlg(message, 'Married?', 'Yes', 'No', 'Yes');
drawnow; % Refresh screen to get rid of dialog box remnants.
if strcmpi(button, 'Yes')
income < rateTable(:, 4)
rowToUse = find(income < rateTable(:, 4), 1, 'first')
deduct = % You do this part
theirRate = % You do this part.
else
income < rateTable(:, 1)
rowToUse = find(income < rateTable(:, 1), 1, 'first')
deduct = % You do this part
theirRate = % You do this part.
end
% Now you compute the tax based on rate and deduct.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!