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
MATLAB: Use elseif inside a case switch. What is Wrong with the code? Only the first “if” case only works none of the elseif work?? Please any advice would help.
case switchelseifif statementMATLAB
Related Question
- How to create a for/ while loop until a condition is met
- Using an if/else statement inside of a for loop
- Am I getting this error “Operands to the || and && operators must be convertible to logical scalar values.”
- Going from switch case construct to loop
- I am trying to use if statements to compare two vectors to get a value from a third. I need to display which months of one vector are higher than another then give the months that they are within .2 of each other.
Best Answer