[英]My if-else statement / switch case statement is not working matlab
我在讓if-else語句/ switch case語句正常工作時遇到麻煩。 我有一個數字,需要查找它是否在一組值或另一組值之間。 我嘗試了各種配置,但沒有任何效果。 (下面的配置示例,為節省空間,PS僅顯示每個配置2個案例,但我有15個案例)
date = 1204 or 1203
switch (date)
case {(1204:1211),(0602:0610)} % (Dec 4-11th) && (Jun 2-10th)
d = 0;
case {(1126:1203),(0611:0618)} % (Nov 26th - Dec 3rd) && (Jun 11-18th)
d = 1;
otherwise
d = 11;
end
switch (date)
case {1204:1211,0602:0610} % (Dec 4-11th) && (Jun 2-10th)
d = 0;
case {1126:1203,0611:0618} % (Nov 26th - Dec 3rd) && (Jun 11-18th)
d = 1;
otherwise
d = 11;
end
switch (date)
case {1204 <= date && date >= 1211,0602 <= date && date >= 0610} % (Dec 4-11th) && (Jun 2-10th)
d = 0;
case {1126 <= date && date >= 1203,0611 <= date && date >= 0618} % (Nov 26th - Dec 3rd) && (Jun 11-18th)
d = 1;
otherwise
d = 11;
end
switch (date)
case {1204 <= date && date >= 1211 || 0602 <= date && date >= 0610} % (Dec 4-11th) && (Jun 2-10th)
d = 0;
case {1126 <= date && date >= 1203 || 0611 <= date && date >= 0618} % (Nov 26th - Dec 3rd) && (Jun 11-18th)
d = 1;
otherwise
d = 11;
end
if 1204 <= date && date >= 1211 || 0602 <= date && date >= 0610 % (Dec 4-11th) && (Jun 2-10th)
d = 0;
elseif 1126 <= date && date >= 1203 || 0611 <= date && date >= 0618 % (Nov 26th - Dec 3rd) && (Jun 11-18th)
d = 1;
else
d = 11;
end
我認為您不能在大小寫表達式中使用條件函數。 而是使用if
/ elseif
/ else
結構:
if (1204<=date && date<=1211) || (0602<=date && date<=0610) % (Dec 4-11th) && (Jun 2-10th)
d = 0;
elseif (1126 <= date && date <= 1203) || (0611 <= date && date <= 0618) % (Nov 26th - Dec 3rd) && (Jun 11-18th)
d = 1;
else
d = 11;
end
從文檔中 :
case_expression
不能包含諸如<
或>
類的關系運算符以與switch_expression
進行比較。 要測試不等式,請使用if
-elseif
語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.