How to create if statements inside switch statement?
4 次查看(过去 30 天)
显示 更早的评论
I don't know if this is possible, but can you put an if statement inside a switch statement?
I am attempting to write this program: 'Write a script to calculate how much to tip a waiter/waitress based on quality of service. The script file should ask for the amount of the bill and whether the service was good (1), fair (2), or poor (3). If the service was good, the tip should be 15% with a minimum tip of £2. If the service was fair, the tip should be 10% with a minimum of £1. If the tip was poor, the tip should be zero. '
I can use the menu function to bring up the choice of service and then generate the tip value using the switch statement. But I am not sure how to get the tip to always be £2 or greater if service is good and £1 or greater if service is fair.
Also, how do I display "The tip is £'tip value'".. I take it I can't use the display function since I want to display a sentence and a variable?
Thanks, Laura
4 个评论
dpb
2013-10-18
编辑:dpb
2013-10-18
Having shown your effort, one alternative amongst many...
Define
>> pct
pct =
0.1500 0.1000 0
>> mins
mins =
2 1 0
Set inputs...
>> svc=1; amt=35;
Compute result...
>> tip=max(amt*pct(svc),mins(svc))
tip =
5.2500
>>
Will want to bound the "service" input and ensure is an integer, make sure amount is positive, etc., etc., for robustness of course.
ADDENDUM:
For the latter, and for "prettifying" the user input in particular, look at
doc inputdlg
doc listdlg
These may be beyond your current instruction level in your class so may not be able to use??? But, they'll solve much of the above very neatly, particularly listdlg for the service level...
采纳的回答
dpb
2013-10-17
a) sure...any coding construct contained within the switch clause can be part of the clause--
switch thevariable
case 1
if something=somethingelse
...
else
...
end
...
case 2
......
b) You may not actually need the if...end construct anyway. Look at min/max and think of the desired result within the switch block...
c) No reason you can't use disp() but it gets to be simpler for more complex output to use fprintf w/ the desired format since w/ disp() you have to build the string output anyway
disp(['The tip is ' num2str(tip)])
as opposed to
fprintf('The tip is %.2f\n', tip)
Note the \n newline character must be added explicitly w/ fprintf, though...
This looking suspiciously like homework, hints provided, not actual code... :)
0 个评论
更多回答(1 个)
Yatin
2013-10-17
Hello Laura,
You can frame 3 different functions for rendering tips (good, fair, poor). Say the function names can be " tipGood ", " tipFair ", " tipPoor ". Then in your switch case based on the option that the user feels for giving the tip, you can call the appropriate function.
Inside the tipGood function you will need to pass the total bill value and calculate 15% of this value and compare this value with 2£ and return the larger of the the two values as a tip from the function. The scenario for the fair tip would also be the same and the tipPoor function would just return 0 as the tip value.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!