Loops question,I think its for loops

1 次查看(过去 30 天)
Hi everyone,
Could someone explain to me how to answer a question such as this:
Write a good which calculates the total cost of materials with one variable being units. If there are less than 1000 units made, the cost is 30c per unit. If there is more than 1000 units sold but less than 2000 units sold it is $100+ 10c per unit. If there is more than 2000 units sold it is $150 +5c per unit. Calculate the total cost of the units and display it as total cost.
Thanks

回答(1 个)

Alex Mcaulley
Alex Mcaulley 2019-7-2
It is not difficult (No loops needed):
function cost = calculateCost(units)
if units < 1000
cost = %write the formula
elseif units > 2000
cost = %write the formula
else
cost = %write the formula
end
end
  2 个评论
Jan
Jan 2019-7-2
编辑:Jan 2019-7-2
Almost. The exact formulation:
if units < 1000
% less than 1000 units
elseif units > 1000 & units < 2000
% more than 1000 units sold but less than 2000 units
elseif units > 2000
% more than 2000
end
Following the instructions, the code fails for units==1000 and ==2000.
"Calculate the total cost of the units and display it as total cost" - A funny formulation also: Can I display the total cost as every other than the total cost?
Nevertheless, Alex, I assume you solved, what was intented. +1
Alex Mcaulley
Alex Mcaulley 2019-7-3
Yes, you are right Jan. Following the statement, units == 1000 and units == 2000 are free XD

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by