我是MATLAB新手(但不是编程新手),在我的工程类中,他们只是教授if/elseif/else和loops的基础知识。那么我们有一个家庭作业,我感到羞愧,我无法弄清楚。我必须在某个地方忽略它的简单性。if-elseif-else语句帮助MATLAB
编写一个程序,询问用户购买螺栓,螺母和垫圈的数量,并计算和打印总数。没关系,我已经完成了这部分。
这里是它变得有点混乱...
作为一个附加的功能,该程序将检查的顺序。正确的顺序必须至少与螺栓一样多,螺母至少是螺栓的两倍,否则顺序有错误。这是程序检查的唯一两个错误:坚果太少,洗衣机太少。对于错误,程序会根据情况打印“检查订单:螺母太少”或“检查订单:洗衣机太少”。如果订单有两个错误,则会打印这两个错误消息。如果没有错误,程序将打印“订单正常”。混淆部分--->如果使用-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);
备注:您的问题陈述要求至少*尽可能多的坚果螺栓,但您正在测试的平等。 – nispio
感谢您提出明确的问题陈述。这并不经常发生! – chappjc