在switch语句的​case_expre​ssion中出现3|​4这样的表达式是什么​意思

17 ビュー (過去 30 日間)
秋实
秋实 2023 年 7 月 24 日
回答済み: Angelo Yeo 2023 年 7 月 25 日
a=1;
switch a
case 3|4
disp('ok');
case {1,2}
disp('perfect');
otherwise
disp('no');
end

回答 (1 件)

Angelo Yeo
Angelo Yeo 2023 年 7 月 25 日
就是或者的意思。但是现在的方式是错的因为3|4总是True的。所以不论a是什么结果就会是ok啊。
3|4
ans = logical
1
您反而要用{}了。看一下以下的例子。
a=1;
switch a
case {3,4}
disp('ok');
case {1,2}
disp('perfect');
otherwise
disp('no');
end
perfect

Community Treasure Hunt

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

Start Hunting!