Switch Case Construction Code help

조회 수: 9 (최근 30일)
Ajay
Ajay 2012년 10월 8일
(Ajay deleted the question, so I (MF) am restoring it.)
I am trying to create a function that takes a cell array containing strings as an input. The string can be triangle, square, pentagon, hexagon, heptagon, octagon, nonagon, or decagon. The output is the sum of the interior angles of the requested shape. If the input is not valid, the code should return a 0. The input should be able to handle cell arrays containing multiple strings and output the corresponding angles as a vector.
I thought I got it, but then I found out I am required to use "switch case" construction, which I have no idea how to do. I tried solving it twice, and both times i got errors. Here are the 2 codes I worked out: What am I doing wrong? I tried reading on switch case construction, and I don't get what's wrong with either code?!
switch InteriorAngle(input)
case 'triangle'
disp ('180')
case 'square'
disp ('360');
case 'pentagon'
disp ('540');
case 'hexagon'
disp ('720');
case 'heptagon'
disp ('900');
case 'octagon'
disp ('1080');
case 'nonagon'
disp ('1260');
case 'decagon'
disp ('1440');
otherwise
disp ('0');
end
CODE 2
function output = InteriorAngle(input)
n=input('s')
switch InteriorAngle
case ('triangle')
n=3;
sum=(n-3).*(180)
case ('square')
n=4;
dispsum=(n-3).*(180)
case('pentagon')
n=5;
sum=(n-3).*(180)
case ('hexagon')
n=6;
sum=(n-3).*(180)
otherwise
disp ('0')
end
  댓글 수: 4
Matt Fig
Matt Fig 2012년 10월 8일
Ajay is particularly bad about this. I personally will be restoring what I can of his old questions and ignoring further questions from Ajay....
Matt Fig
Matt Fig 2012년 10월 9일
Saved from google cache:
I am trying to create a function that takes a cell array containing strings as an input. The string can be triangle, square, pentagon, hexagon, heptagon, octagon, nonagon, or decagon. The output is the sum of the interior angles of the requested shape. If the input is not valid, the code should return a 0. The input should be able to handle cell arrays containing multiple strings and output the corresponding angles as a vector.
I thought I got it, but then I found out I am required to use "switch case" construction, which I have no idea how to do. I tried solving it twice, and both times i got errors. Here are the 2 codes I worked out: What am I doing wrong? I tried reading on switch case construction, and I don't get what's wrong with either code?!
switch InteriorAngle(input) case 'triangle' disp ('180') case 'square' disp ('360'); case 'pentagon' disp ('540'); case 'hexagon' disp ('720'); case 'heptagon' disp ('900'); case 'octagon' disp ('1080'); case 'nonagon' disp ('1260'); case 'decagon' disp ('1440'); otherwise disp ('0'); end
CODE 2
function output = InteriorAngle(input) n=input('s') switch InteriorAngle case ('triangle') n=3; sum=(n-3).*(180) case ('square') n=4; dispsum=(n-3).*(180) case('pentagon') n=5; sum=(n-3).*(180) case ('hexagon') n=6; sum=(n-3).*(180) otherwise disp ('0') end

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 8일
편집: Azzi Abdelmalek 2012년 10월 8일
switch input('InteriorAngle')
case 'triangle'
disp ('180')
case 'square'
disp ('360');
case 'pentagon'
disp ('540');
case 'hexagon'
disp ('720');
case 'heptagon'
disp ('900');
case 'octagon'
disp ('1080');
case 'nonagon'
disp ('1260');
case 'decagon'
disp ('1440');
otherwise
disp ('0');
end
  댓글 수: 5
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 8일
편집: Azzi Abdelmalek 2012년 10월 8일
function output = InteriorAngle(s)
switch s
case ('triangle')
n=3;
sum=(n-3).*(180)
case ('square')
n=4;
dispsum=(n-3).*(180)
case('pentagon')
n=5;
sum=(n-3).*(180)
case ('hexagon')
n=6;
sum=(n-3).*(180)
otherwise
disp ('0')
end
%
InteriorAngle('triangle')
Ajay
Ajay 2012년 10월 8일
OH i see i see. thanks :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Microsoft .NET에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by