Filter löschen
Filter löschen

Why is this returning "logical 1"

5 Ansichten (letzte 30 Tage)
Rachel Dawn
Rachel Dawn am 7 Feb. 2018
Bearbeitet: Matt J am 7 Feb. 2018
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 Kommentar
Rachel Dawn
Rachel Dawn am 7 Feb. 2018
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?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Matt J
Matt J am 7 Feb. 2018
Bearbeitet: Matt J am 7 Feb. 2018
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

Community Treasure Hunt

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

Start Hunting!

Translated by