用matlab程序switch...case语句得出各月份的季节(如3,4,5月输出为春季)
时间: 2024-09-30 07:10:46 浏览: 34
在MATLAB中,你可以使用`switch`...`case`语句结合数组索引来实现根据月份判断季节的功能。下面是一个简单的例子:
```matlab
months = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; % 定义月份数组
seasons = cell(1, length(months)); % 创建空的字符串向量存储季节
% 使用switch...case结构
for i = 1:length(months)
switch months(i)
case 3: seasons{i} = '春季';
case 4: seasons{i} = '春季';
case 5: seasons{i} = '春季';
case 6: seasons{i} = '夏季';
case 7: seasons{i} = '夏季';
case 8: seasons{i} = '夏季';
case 9: seasons{i} = '秋季';
case 10: seasons{i} = '秋季';
case 11: seasons{i} = '秋季';
case 12: seasons{i} = '冬季';
otherwise: seasons{i} = '未知';
end
end
% 输出结果
disp("月份对应的季节:");
for i = 1:length(months), disp(sprintf('%d - %s', months(i), seasons{i})), end
阅读全文