Test for integer value

조회 수: 8 (최근 30일)
Corey Washburn
Corey Washburn 2018년 9월 28일
편집: Bruno Luong 2018년 9월 29일
I am trying to make a battleship styled game, one of the parts involve getting a set of user input 'firing coordinates', which I then need to test to make sure it is an integer value 1 through 10, what function can I use to test that it is an integer?
*edit: I am looking for a logical operator or else condition that can specify a non integer input.

채택된 답변

James Tursa
James Tursa 2018년 9월 28일
편집: James Tursa 2018년 9월 28일
See the following:
doc fix
doc ceil
doc floor
doc round
E.g., for a scalar x
if( round(x) == x )
% x is an integer
  댓글 수: 2
Corey Washburn
Corey Washburn 2018년 9월 28일
I am not looking for a way to make a decimal value into an integer (round 4.3 to 4) I am looking for a logical operator that is false if the input is not an integer value.
James Tursa
James Tursa 2018년 9월 29일
That's exactly what my code does ...

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

추가 답변 (2개)

Bruno Luong
Bruno Luong 2018년 9월 29일
편집: Bruno Luong 2018년 9월 29일
Perhaps no better no worse than James's solution just different
iswholenumber = mod(x,1)==0

Bruno Luong
Bruno Luong 2018년 9월 29일
편집: Bruno Luong 2018년 9월 29일
according to my test slightly faster, but need to assume the smaller range of x of be working:
x == int32(x)

카테고리

Help CenterFile Exchange에서 Antennas, Microphones, and Sonar Transducers에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by