how can i use multiple expression values in If-then

조회 수: 2 (최근 30일)
Syed Kaleem Gilani
Syed Kaleem Gilani 2013년 12월 13일
편집: Syed Kaleem Gilani 2013년 12월 14일
I will post my code that i made, im a noob. just take my "||" as or in english, thats what i think of it, but it dont work, it only gives me 'NORTH' for any value please check and correct..It is m,y university assignment.
function[r]=direction(bearing)
if bearing == 0 || 360
r= 0 || 360;
printf('North\n')
elseif bearing== 90 || -270
r= 90 || -120;
printf('East\n')
elseif bearing== 180 || -180
r= 180 || -180;
printf('South\n')
elseif bearing== 270 || -90
r= 270 || -90;
printf('West\n')
else
printf('Unknown Direction\n')
end

채택된 답변

Syed Kaleem Gilani
Syed Kaleem Gilani 2013년 12월 14일
편집: Syed Kaleem Gilani 2013년 12월 14일
I found the solution, BTW thank you so much @paul and @ jos(10584)
function[]=direction(bearing)
if (bearing == 0 || bearing==360)
disp('North\n')
elseif (bearing== 90 || bearing ==-270)
disp('East\n')
elseif (bearing== 180 || bearing== -180)
disp('South\n')
elseif (bearing== 270 || bearing== -90)
disp('West\n')
else
disp('Unknown Direction\n')
end
%it works fine

추가 답변 (2개)

Jos (10584)
Jos (10584) 2013년 12월 13일
This should get you started:
if A==1 || A == 2
disp('A is 1 or 2') ;
end
if B == 1 && C == 2
disp('B is 1 and C is 2') ;
end
  댓글 수: 1
Jos (10584)
Jos (10584) 2013년 12월 13일
btw , the reason why you get 'North' all the time is because the condition
bearing == 0 || 360
is equal
bearing == 0 || true
which is always true of course.

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


Paul
Paul 2013년 12월 13일
function[r]=direction(bearing)
if bearing == 0 | | bearing==360
r= 0;
fprintf('North\n')
elseif bearing== 90 | | bearing==-270;
r= 90;
fprintf('East\n')
elseif bearing== 180 | | bearing== 180;
r= 180;
fprintf('South\n')
elseif bearing== 270 | | bearing== -90 ;
r= 270;
fprintf('West\n') ;
else printf('Unknown Direction\n')
end
I think that's what you want unless you want to define r as a string ?

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by