我想在MATLAB中为想要使用“输入”功能订购披萨以询问一些选项的用户编写一个小菜单。例如,我可以先询问披萨是否应该是素食者,如果1 =是,那么我会要求不同类型的蔬菜,如果2 =没有要求不同的肉类选择等。
到目前为止,我尝试使用'if / else if / else'循环:我创建了4个菜单:vegetrian(2个菜单)和Nonvegetrian(2个菜单),如下所示,但是,在某些步骤中它不起作用。有人可以帮我吗?
a=input('Vegetrian: ');
a1=input('Mix Vegetable:');
a2=input('SAAG:')
b=input('Nonvegetrian: ');
b1=input('chicken PIZZA:');
b2=input('Cheese PIZZA:')
if a==1
disp(a1);
elseif a1==2
disp(b);
elseif b==1
disp(b1);
elseif b1==2
dip(b2);
else
disp('Not Available');
end
答案 0 :(得分:0)
选择所需的逻辑稍微复杂一些。
可能的实施可能是:
%
% Define the types of Pizza
%
veg_type{1}='Mix Vegetable:';
veg_type{2}='SAAG';
non_veg{1}='Chicken Pizza';
non_veg{2}='Cheese Pizza';
%
% Ask for Vegetarian Pizza
%
a=input('Which Pizza do you want? Vegetarian = 1, Non Vegetarian = 2): ');
%
% Analyse the selection
%
% Vegetarian options
%
if(a == 1)
sprintf('Available Vegetarian Pizza: \n\t%s=1\n\t%s=2' ,veg_type{1},veg_type{2})
a1=input('Select your Vegetarian Pizza: ');
if(a1 == 1)
disp('You have select a Mix Vegetable Pizza')
elseif(a1 == 2)
disp('You have select a SAAG Pizza')
else
disp('Invalid Selection: Pizza not available')
end
elseif(a == 2)
%
% Non Vegetarian options
%
sprintf('Available Non Vegetarian Pizza: \n\t%s=1\n\t%s=2' ,non_veg{1},non_veg{2})
b1=input('Select your Non Vegetarian Pizza: ');
if(b1 == 1)
disp('You have select a Chicken Pizza')
elseif(b1 == 2)
disp('You have select a Cheese Pizza')
else
disp('Invalid Selection: Pizza not available')
end
%
% Neither Vegetarian nor Non Vegetarian
%
else
disp('So you do not want neither a Vegetarian nor a Non Vegetarian Pizza')
end
希望这有帮助。
Qapla'