Use elseif inside a case switch. What is Wrong with my code? Only the first "if" case only works none of the elseif work?? Please any advice would help.
Afficher commentaires plus anciens
clc
clear all
disp ('Cody,Ward')
disp ('EG167, 07')
disp('Assignment #12')
disp(' ')
disp ('Problem: #21')
disp(' ')
rp=input('What is the rental period for the car (days): ');
cartype=input('What type of car class (B, C, D) ?: ','s');
switch cartype
case 'B'
if (1<rp) && (rp<6);
cost=rp*27;
fprintf('\nThe car rental cost is $%3.2f.\n\n',cost')
elseif rp==7;
n=162
cost=n
fprintf('\nThe car rental cost is $%3.2f.\n\n',cost')
elseif (7<rp) && (rp<27);
n=rp-7;
cost=162+(n*25);
fprintf('\nThe car rental cost is $%3.2f.\n\n',cost')
elseif rp==28;
n=662;
cost=n;
fprintf('\nThe rental cost is $%3.2f.\n\n',cost')
elseif (28<rp) && (rp<=60);
n=rp-28;
cost=662+(n*23);
fprintf('\nThe rental cost is $%3.2f.\n\n',cost')
else rp>60;
fprintf('\nRental is not available for more than 60 days.\n\n')
end
case 'C'
if (1<rp) && (rp<6);
cost=rp*34;
fprintf('\nThe car rental cost is $%3.2f.\n\n',cost')
elseif rp==7;
n=204
cost=n
fprintf('\nThe car rental cost is $%3.2f.\n\n',cost')
elseif (7<rp) && (rp<27);
n=rp-7;
cost=162+(n*31);
fprintf('\nThe car rental cost is $%3.2f.\n\n',cost')
elseif rp==28;
n=824;
cost=n;
fprintf('\nThe rental cost is $%3.2f.\n\n',cost')
elseif (28<rp) && (rp<=60);
n=rp-28;
cost=824+(n*28);
fprintf('\nThe rental cost is $%3.2f.\n\n',cost')
else rp>60;
fprintf('\nRental is not available for more than 60 days.\n\n')
end
case 'D'
if (1<rp) && (rp<6);
fprintf('\nClass D cannot be rented for less than 7 days.\n\n')
elseif rp==7;
n=276
cost=n
fprintf('\nThe car rental cost is $%3.2f.\n\n',cost')
elseif (7<rp) && (rp<27);
n=rp-7;
cost=204+(n*31);
fprintf('\nThe car rental cost is $%3.2f.\n\n',cost')
elseif rp==28;
n=1136;
cost=n;
fprintf('\nThe rental cost is $%3.2f.\n\n',cost')
elseif (28<rp) && (rp<=60);
n=rp-28;
cost=1136+(n*28);
fprintf('\nThe rental cost is $%3.2f.\n\n',cost')
else rp>60;
fprintf('\nRental is not available for more than 60 days.\n\n')
end
end
Réponse acceptée
Plus de réponses (1)
Paulo Silva
le 3 Mai 2011
This condition
elseif 7<rp<=27;
is valid for rp=27 so why do you have this one
elseif rp==27;
after it?
7 commentaires
Cody
le 3 Mai 2011
Cody
le 3 Mai 2011
Paulo Silva
le 3 Mai 2011
you still need to fix the code for the error that Teja found, even in the elseif lines you still got the error.
Cody
le 3 Mai 2011
Paulo Silva
le 3 Mai 2011
you also don't need to put ; in the if and elseif
Cody
le 3 Mai 2011
Paulo Silva
le 3 Mai 2011
if (1<rp) && (rp<6); -> if (1<rp) && (rp<6)
Catégories
En savoir plus sur Debugging and Improving Code dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!