继续温度转换,这次换Switch实现
上一篇if else实现请移步 https://www.cnblogs.com/Wendows/p/14403443.html
温度转换 version 2.0
代码实现
%Temperature format conservation
% Version 2.0
loopflag = 1; %循环标志位初始化
modeflag = 0; %工作模式标志位初始化,默认不选择工作方式
vertips = 'TFC(version 2.0)';
disp(vertips);
tips = '模式0退出,模式1华氏温度转摄氏温度,模式2摄氏温度转华氏温度';
%modelflag==0退出,==1华氏温度转摄氏温度,==2摄氏温度转华氏温度
while(loopflag ~= 0)
disp(tips);
modeflag = input('Please select working mode(1 or 2): ');
modeflag = num2str(modeflag);
switch modeflag
case '1'
Ftempin = input('Please type the temperature in F: ');
Ctemp = (Ftempin - 32)/1.8;
Ctemp = num2str(Ctemp);
outtips = 'The temperature in C is: ';
Ctempout = [outtips,Ctemp];
disp(Ctempout);
clearvars -except loopflag tips;
case '2'
Ctempin = input('Please type the temperature in C: ');
Ftemp = Ctempin*1.8+32;
Ftemp = num2str(Ftemp);
outtips = 'The temperature in F is: ';
Ftempout = [outtips,Ftemp];
disp(Ftempout);
clearvars -except loopflag tips;
otherwise
loopflag = 0;
modeflag = 0;
end
end
clear;
这里要说明的地方就一个,调用num2str()是为了匹配,这样可以识别判断除数字外的其他输入