How to create if statements inside switch statement?

17 vues (au cours des 30 derniers jours)
Laura
Laura le 17 Oct 2013
Commenté : dpb le 18 Oct 2013
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 commentaires
Laura
Laura le 18 Oct 2013
I just got it working by doing this:
bill=input('Please enter bill amount:£');
fprintf('1. Good \n');
fprintf('2. Fair \n');
fprintf('3. Poor \n');
service=input('Please enter standard of service code:');
switch service
case {1}
tip=(15/100)*bill;
if tip<2;
tip=2;
end
case {2}
tip=(10/100)*bill;
if tip<1;
tip=1;
end
case {3}
tip=0;
end
fprintf('Tip value is £%.2f\n',tip);
Is there an easier way to do this?
dpb
dpb le 18 Oct 2013
Modifié(e) : dpb le 18 Oct 2013
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...

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 17 Oct 2013
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... :)

Plus de réponses (1)

Yatin
Yatin le 17 Oct 2013
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.

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by