Having problem in using or statement in switch case

조회 수: 2 (최근 30일)
Arif Istiak Abeg
Arif Istiak Abeg 2020년 5월 7일
댓글: Arif Istiak Abeg 2020년 5월 19일
Problem: "Return the number of days in a given month in 2017. If the input is not a valid month number, return -1.
You cannot use IF statements"
Where is the problem in my code.How can I use or statement in switch case?
Firsly I tried case as number but it didn't work
then I tried as character but it also didn't work
function d = daysInMonth(m)
x=[31 28 31 30 31 30 31 31 30 31 30 31];
c = num2str(m);
switch c
case '1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'|'10'|'11'|'12'
d=x(m);
otherwise
d=-1;
end
end

답변 (1개)

Walter Roberson
Walter Roberson 2020년 5월 7일
case {'1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'}
However, you do not need to convert to character vector.
switch m
case {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
end
  댓글 수: 1
Arif Istiak Abeg
Arif Istiak Abeg 2020년 5월 19일
I tried this code also but didn,t work
then I used 'case" for 12 line indiviually it worked but the code became longer

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Fuzzy Logic Toolbox에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by