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"
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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!!
Respuestas (1)
Matt J
el 7 de Feb. de 2018
Editada: Matt J
el 7 de Feb. de 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
0 comentarios
Ver también
Categorías
Más información sobre Functions en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!