Fahrenheit function but confused on the output.

1 回表示 (過去 30 日間)
Ireedui Ganzorig
Ireedui Ganzorig 2020 年 3 月 18 日
コメント済み: Shannon Wagoner 2020 年 4 月 8 日
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 Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by