以K或F表示的温度的Matlab脚本
编写一个脚本,提示用户以摄氏度为单位输入温度,然后以华氏度为单位输入F,以开尔文为单位输入K。该脚本将以用户指定的刻度打印相应的温度。例如,输出mght如下所示: 输入温度,单位为摄氏度:29.3 你想要K还是F?F 温度单位为华氏84.7度 这是我的尝试,但运行不好,我想更正一下以K或F表示的温度的Matlab脚本,matlab,Matlab,编写一个脚本,提示用户以摄氏度为单位输入温度,然后以华氏度为单位输入F,以开尔文为单位输入K。该脚本将以用户指定的刻度打印相应的温度。例如,输出mght如下所示: 输入温度,单位为摄氏度:29.3 你想要K还是F?F 温度单位为华氏84.7度 这是我的尝试,但运行不好,我想更正一下 % this for prompting a user for a temp in C and %print a temp either in K or F as specified by the user C=i
% this for prompting a user for a temp in C and
%print a temp either in K or F as specified by the user
C=input('Enter temperature in degrees C:');
%user specification
j=input('Do you want K or F?');
%error check
while j~=K || j~=F
disp('Please enter K or F')
j=input('Enter K or F now:');
end
if j==F
F=9/5*C+32;
fprintf('The temperature in degrees F is %.2f\n',F);
else
K=C+273.15;
fprintf('The temperature in degrees Kis %.2f\n',K);
end
我现在没有matlab,但请尝试以下方法:
C=input('Enter temperature in degrees C:');
% the j should be returned as string
j=input('Do you want K or F?','s');
% You should compare string j 'K' or 'F' - in Your case the K and F was some uninitialized variables
while j~='K' && j~='F'
disp('Please enter K or F')
% again return string
j=input('Enter K or F now:','s');
end
%compare to char F
if (j=='F')
F=9/5*C+32;
fprintf('The temperature in degrees F is %.2f\n',F);
% compare to char 'K'
elseif (j=='K')
K=C+273.15;
fprintf('The temperature in degrees Kis %.2f\n',K);
end
首先使用'elseif(j==K'),这样只有按K才能得到K中的温度。顺便问一下,这意味着运行不好吗?张贴一些examples@akfaz:对不起,我的意思是它根本不运行。谢谢,让我现在通过实现来尝试。好的,我发现了错误。在while语句中,而不是| |应为&(我更新了代码)