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.
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
Paulo Silva
2011년 5월 3일
This condition
elseif 7<rp<=27;
is valid for rp=27 so why do you have this one
elseif rp==27;
after it?
댓글 수: 7
Cody
2011년 5월 3일
Cody
2011년 5월 3일
Paulo Silva
2011년 5월 3일
you still need to fix the code for the error that Teja found, even in the elseif lines you still got the error.
Cody
2011년 5월 3일
Paulo Silva
2011년 5월 3일
you also don't need to put ; in the if and elseif
Cody
2011년 5월 3일
Paulo Silva
2011년 5월 3일
if (1<rp) && (rp<6); -> if (1<rp) && (rp<6)
카테고리
도움말 센터 및 File Exchange에서 Debugging and Improving Code에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!