필터 지우기
필터 지우기

Problem with an if statement

조회 수: 3 (최근 30일)
Tariq Hammoudeh
Tariq Hammoudeh 2021년 12월 30일
댓글: Voss 2022년 1월 2일
I have this code:
orientation= input (' enter v or h')
if orientation == v
......
end
but whenever i run the code and enter v or h, I get an error saying
Unrecognized function or variable 'v'.
So does matlab not take inputs in letters or is there a way to make it work.

채택된 답변

Voss
Voss 2021년 12월 30일
Use the optional second argument to the input() function, 's', which tells input() to return exactly what the user entered, without evaluating it. You'll also need to compare orientation to 'v' rather than v or you'll just get the same error on the next line (and use strcmp/strcmpi in case they entered more than one character or an empty string).
orientation= input (' enter v or h', 's')
if strcmp(orientation,'v')
% do v stuff
else
% do h stuff
end
  댓글 수: 11
Tariq Hammoudeh
Tariq Hammoudeh 2022년 1월 2일
편집: Tariq Hammoudeh 2022년 1월 2일
@Benjamin Again thank you i added this to my other code and it works fine, but i just want to ask what does the for loop do, I understood everything else but i just want to know what does this for loop do.
Voss
Voss 2022년 1월 2일
The for loop is to loop over the different ships, asking the user for each ship's startingGrid and orientation and placing each ship on the board.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by