if loop for checking package weights and to calculate shipping costs

6 ビュー (過去 30 日間)
dat
dat 2023 年 11 月 16 日
回答済み: Star Strider 2023 年 11 月 16 日
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

採用された回答

Star Strider
Star Strider 2023 年 11 月 16 日
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!
.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by