您当前的位置: 首页 >  matlab

MATLAB 用switch语句实现选择结构 例题

发布时间:2022-01-08 21:24:03 ,浏览量:6

MATLAB 用switch语句实现选择结构 例题

解释代码:

x=input('x=?'); switch fix(x) case 2 disp(111); case 2 disp(222); case{3,4,5} disp(333); otherwise
        disp(444); end

eg:输入一个英文单词,判断它是否以元音字母开头。

方法一:

c=input('请输入一个单词:','s'); switch c(1) case{'A','E','I','O','U','a','e','i','o','u'} disp([c,'以元音字母开头']); otherwise
        disp([c,'以辅音字母开头']); end

方法二:

c=input('请输入一个单词:','s'); if findstr(c(1),'AEIOUaeiou')>0 disp([c,'以元音字母开头']); else disp([c,'以辅音字母开头']); end

例题:

在这里插入图片描述 代码如下:

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
关注
打赏
1688896170
查看更多评论

暂无认证

  • 6浏览

    0关注

    115984博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0586s