Problem with an if statement
1 回表示 (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
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 件のコメント
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 Center および File Exchange で Conway's Game of Life についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!