I wanna write same program by using switch , but how ?

I wanna write same program by using switch , but how ?:
x=input("inter your age :");
if x>=13 && x<18
disp("person is teenager")
disp("not eligible for voating")
else
disp("person is not teenager")
if x>=18
disp("eligible for voating ")
else
disp("not eligible for voating")
end
end

 采纳的回答

x = input("inter your age :");
switch(true)
case x>=13 && x<18
disp("person is teenager")
disp("not eligible for voating")
case x > 18
disp("person is not teenager")
disp("eligible for voting");
otherwise
disp("person is not teenager");
disp("not eligible for vating")
end
or
x = input("inter your age :");
switch x>=13 && x<18
case true
disp("person is teenager")
disp("not eligible for voating")
otherwise
disp("person is not teenager")
switch x>=18
case true
disp("eligible for voating ")
otherwise
disp("not eligible for voating")
end
end
In the special case that your input was certain to be a non-negative integer (which is not the situation now -- users can enter negatives or fractions or vectors)
switch x
case 0:12
disp("person is not teenager")
disp("not eligible for voating")
case 13:18
disp("person is teenager")
disp("not eligible for voating")
otherwise
disp("person is not teenager");
disp("eligible for vating")
end

2 个评论

Thank you very much 🙏🌸🤍🌸
I appreciate that Your life is full of joy and light🙏🤍🙏

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by