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?
Why is this returning "logical 1"
5 ビュー (過去 30 日間)
古いコメントを表示
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 件)
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
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!