if loop for checking package weights and to calculate shipping costs
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
dat
am 16 Nov. 2023
Beantwortet: Star Strider
am 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 Kommentare
Akzeptierte Antwort
Star Strider
am 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
I will let you explore the changes I made to understand what I did and the reason the logic now works!
.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!