Fahrenheit function but confused on the output.

1 次查看(过去 30 天)
Hello MATLAB community,
I feel like I did everything right, but the function output is really confusing me. Or did I do everything wrong in the first place?
This is function output.
0 = solid
1 = liquid
2 = gas
Below is my code.
function [state] = h2oState(tempF)
C = input('Please enter your Celsius value to be converted into Fahrenheit ');
tempF = 9/5 * C + 32;
if tempF <= 32
disp(['The Fahrenheit temperature is ',num2str(tempF)])
disp('solid')
elseif tempF > 212
disp(['The Fahrenheit temperature is ',num2str(tempF)])
disp('gas')
else
disp(['The Fahrenheit temperature is ',num2str(tempF)])
disp('liquid')
end
end

回答(1 个)

Rik
Rik 2020-3-18
You are overwriting the input to your function by asking the user for the temperature in Celsius. You are also not assigning any value to your output variable state.
You probably want something like this:
function state = h2oState(C)
tempF = 9/5 * C + 32;
if tempF <= 32
disp(['The Fahrenheit temperature is ',num2str(tempF)])
disp('solid')
state=0;
elseif tempF > 212
disp(['The Fahrenheit temperature is ',num2str(tempF)])
disp('gas')
state=2;
else
disp(['The Fahrenheit temperature is ',num2str(tempF)])
disp('liquid')
state=1;
end
end

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by