フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

The case of the odd case statement

2 ビュー (過去 30 日間)
Steve D
Steve D 2011 年 8 月 17 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
So I entered the following code and tried it with the dispaly(r) either commented out or not. Its just trying to try all case statement cases
Both ways seemed to behave wrong. See for yourself.
daylight = 1; while daylight whatever=1; while whatever r = fix(2.99 *rand) + 1; % n=1..3 %display(r); switch r case r==1 if whatever display('case 1'); end case r==2 if whatever display('case 2'); end otherwise display('otherwise'); whatever=0; end end end
% GO Figure! What am I doing wrong?
  1 件のコメント
Jan
Jan 2011 年 8 月 17 日
Please format your code, see: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup

回答 (2 件)

Jan
Jan 2011 年 8 月 17 日
This problem appears frequently in the last weeks.
"case r==2" does not trigger for r==2, but it is equivalent to "case 1" if r==2 and "case 0" otherwise. As you can see in the documentation of switch, the correct syntax is:
switch r
case 1
...
case 2
...
otherwise
...
end
  1 件のコメント
Steve D
Steve D 2011 年 8 月 18 日
Thanks Jan, that got it. sd

Steve D
Steve D 2011 年 8 月 18 日
%sorry for the missing indents before. daylight = 1; while daylight whatever=1; while whatever r = fix(2.99 *rand) + 1; % n=1..3 %display(r); switch r case r==1 if whatever display('case 1'); end case r==2 if whatever display('case 2'); end otherwise display('otherwise'); whatever=0; end end end
  1 件のコメント
Jan
Jan 2011 年 8 月 18 日
A repeation does not necessarily mean an improvement ;-)
You could edit the original question and delete this answer.

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by