Borrar filtros
Borrar filtros

if loop for checking package weights and to calculate shipping costs

6 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Star Strider
Star Strider el 16 de Nov. de 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
Your total cost is $15.00
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!
.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by