Fahrenheit function but confused on the output.

2 vues (au cours des 30 derniers jours)
Ireedui Ganzorig
Ireedui Ganzorig le 18 Mar 2020
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

Réponses (1)

Rik
Rik le 18 Mar 2020
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

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by