필터 지우기
필터 지우기

Fahrenheit function but confused on the output.

조회 수: 2 (최근 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 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