필터 지우기
필터 지우기

Why is this returning "logical 1"

조회 수: 5 (최근 30일)
Rachel Dawn
Rachel Dawn 2018년 2월 7일
편집: Matt J 2018년 2월 7일
This is my code:
userAge = input('How old are you?');
if userAge >0 && userAge <7
disp('Oh that''s so cute!')
elseif userAge >= 7 && userAge < 23
disp('What school do you go to?')
elseif userAge >= 23 && userAge < 65
disp('Do you work?')
elseif userAge >= 65 && userAge <=110
disp ('Are you retired yet?')
elseif userAge <0
disp ('That''s impossible.')
else userAge > 110
disp('That''s highly improbable.')
end
When I enter any number that is over 110 (like 112 or 956), Matlab returns this:
How old are you? 956
ans =
logical
1
That's highly improbable.
But with any other number, the "logical 1" does not come out. Is there something wrong with my code?
thank you!!
  댓글 수: 1
Rachel Dawn
Rachel Dawn 2018년 2월 7일
Wait just kidding. I got it. It's because I have "else" on that line rather than "elseif". So then when would we ever use else? When we don't have any condition at all?

댓글을 달려면 로그인하십시오.

답변 (1개)

Matt J
Matt J 2018년 2월 7일
편집: Matt J 2018년 2월 7일
You might use ELSE as follows,
userAge = input('How old are you?');
if userAge >0 && userAge <7
disp('Oh that''s so cute!')
elseif userAge >= 7 && userAge < 23
disp('What school do you go to?')
elseif userAge >= 23 && userAge < 65
disp('Do you work?')
elseif userAge >= 65 && userAge <=110
disp ('Are you retired yet?')
elseif userAge <0
disp ('That''s impossible.')
else
disp('That''s highly improbable.')
end

카테고리

Help CenterFile Exchange에서 Automated Driving Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by