hax*_*ode 2 matlab if-statement
我是MATLAB的新手(但不是编程新手),在我的工程课中,他们只是教授if/elseif/else和循环的基础知识.好吧,我们有一个家庭作业,我感到惭愧,我无法弄清楚.我必须在某处忽略它的简洁性.
编写一个程序,询问用户购买时螺栓,螺母和垫圈的数量,并计算和打印总数.那没关系,我已经完成了这一部分.
这里有点让人困惑......
作为附加功能,程序会检查订单.正确的订单必须至少具有与螺栓一样多的螺母和至少两倍于螺栓的垫圈,否则订单会出错.这是程序检查的唯一两个错误:螺母太少,垫圈太少.如果错误,程序会根据需要打印"检查订单:螺母太少"或"检查订单:太少的垫圈".如果订单同时存在错误,则会打印两条错误消息.如果没有错误,程序将打印"订单正常".CONFUSING PART --->只需使用一组if -elseif-else语句即可完成此操作.
如果两个都是真的,我怎么能用if-elseif-else语句打印这个程序呢?
这是我的代码:
% Get the amount each part
bolts = input('Enter the number of bolts: ');
nuts = input('Enter the number of nuts: ');
washers = input('Enter the number of washers: ');
% Check for the correct amount of nuts, bolts, and washers
if bolts ~= nuts
disp('Check order: too few nuts');
elseif bolts * 2 ~= washers
disp('Check order: too few washers');
else
disp('Order is OK.');
end
% Calculate the cost of each of the parts
costOfBolts = bolts * .05;
costOfNuts = nuts * .03;
costOfWashers = washers * .01;
% Calculate the total cost of all parts
totalCost = costOfBolts + costOfNuts + costOfWashers;
% Print the total cost of all the parts
fprintf('Total cost: %.2f\n', totalCost);
Run Code Online (Sandbox Code Playgroud)