% 慕课matlab学习 第三章-014
% 02-用switch语句实现选择结构
% 第三节 程序流程控制
% 02-用switch语句实现选择结构
% switch case 和 otherwise
x=input('请输入x 的值 X=?')
switch fix(x)
case 2 % case 2 实际
disp(111);
case 3
disp(222) % 3
case {1,2,4,5} % 1,2,4 ,5 四个条件可以运行 { } 包含多个分支
disp(333)
otherwise
disp(444)
end
% eg
% 例1 输入一个英文单词,判断它是否以元音字母开头。
c=input('请输入一个单词:' ,'S')
switch c(1)
case{'A','E','I','O','U','a','e','i','o','u'}
disp([c,'以元音字母开头'])
otherwise
disp([c,'以辅音字母开头'])
end
% 使用switch-case 表达式来进判断空气质量好坏
g=input('请输入PM2.5值: ')
switch fix(g)
case num2cell(0:34)
disp('空气质量优')
case num2cell(35:74)
disp('空气质量良好')
case num2cell(75:114)
disp('空气质量轻度污染')
case num2cell(115:149)
disp('空气质量中度污染')
case num2cell(150:249)
disp('空气质量重度污染')
otherwise
disp('空气质量严重污染')
end