MATLAB 编程:循环、条件语句与根查找全解析
1. 循环与条件语句基础
在 MATLAB 编程中,循环和条件语句是非常重要的基础内容。下面通过几个示例来详细介绍。
1.1 月份天数计算示例
以下代码用于根据用户输入的月份前三个字母来计算该月的天数:
msg = 'Enter first three letters of the month: ';
month = input(msg,'s');
month = month(1:3); % Just use the first three letters
if lower(month) == 'feb'
leap = input('Is it a leap year (y/n): ','s');
end
switch lower(month)
case {'sep', 'apr', 'jun', 'nov'}
days = 30;
case 'feb'
switch lower(leap)
case 'y'
days = 29;
otherwise
days = 28;
end
otherwise
days = 31;
end
- 代码解释 :
- 首先,通过
input函数获取用户输入的月份前三
- 首先,通过
超级会员免费看
订阅专栏 解锁全文
2256

被折叠的 条评论
为什么被折叠?



