# if loop for checking package weights and to calculate shipping costs

2 vues (au cours des 30 derniers jours)
dat le 16 Nov 2023
package = input('Enter weight of package\n')
cost =15; % %15 for the two first lbs
if package <=2 %checks for packages equal or less than 2lbs
fprintf('Your total cost is \$%.2f \n', cost)
elseif package >=70 % checks for packages over 70lbs
cost = cost + 15; % adds %15.00 when over 70 lbs
fiveperlb = (package - 2) * 5; %calculates additional %5.00 per lb after initial 2lbs
total_shipping = cost + fiveperlb; %sums up the total shipping cost
fprintf('The total shipping cost is \$%.2f',total_shipping)
elseif package < 70 % checks for packages less than 70 lbs
fiveperlb2 = (package -2) * 5; %calculates additional %5.00/lb after initial 2lbs
total_shipping2 = cost + fiveperlb2; % sums up total shipping costs
fprintf('The total shipping cost is \$%.2f', total_shipping2)
else package > 100
disp('Package weight exceeded, will not accept')
end
I can't seem to figure out how to get it to decline me when i input a weight higher than 100 lbs
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Star Strider le 16 Nov 2023
Try this —
for package = [1 25 50 75 110]
package % Package Weight In Loop (Delete When Loop No Longer NEcessary)
% package = input('Enter weight of package\n')
cost =15; % %15 for the two first lbs
if package <=2 %checks for packages equal or less than 2lbs
fprintf('Your total cost is \$%.2f \n', cost)
elseif package >=70 & package < 100 % checks for packages over 70lbs
cost = cost + 15; % adds %15.00 when over 70 lbs
fiveperlb = (package - 2) * 5; %calculates additional %5.00 per lb after initial 2lbs
total_shipping = cost + fiveperlb; %sums up the total shipping cost
fprintf('The total shipping cost is \$%.2f',total_shipping)
elseif package < 70 % checks for packages less than 70 lbs
fiveperlb2 = (package -2) * 5; %calculates additional %5.00/lb after initial 2lbs
total_shipping2 = cost + fiveperlb2; % sums up total shipping costs
fprintf('The total shipping cost is \$%.2f', total_shipping2)
else % package > 100
disp('Package weight exceeded, will not accept')
end
end
package = 1
package = 25
The total shipping cost is \$130.00
package = 50
The total shipping cost is \$255.00
package = 75
The total shipping cost is \$395.00
package = 110
Package weight exceeded, will not accept
I will let you explore the changes I made to understand what I did and the reason the logic now works!
.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Programming 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