checking that a user input is an integer
조회 수: 8 (최근 30일)
이전 댓글 표시
The user is told to input an integer between 1 and 59 but I want an error message to display if the value they enter is not an integer. This is what I have so far:
prompt = {'Enter your first integer between 1 and 59'};
dlg_title = 'User inputs';
num_lines = 1;
answer = inputdlg(prompt,dlg_title,num_lines);
u=str2double(answer{1});
댓글 수: 0
채택된 답변
Star Strider
2018년 2월 19일
There are several ways of testing to see if the returned value is an integer.
You want to see if there is any non-zero value to the right of the decimal, so test for that. You can also experiment with the rem or mod functions.
댓글 수: 0
추가 답변 (1개)
Roger Stafford
2018년 2월 19일
Test whether this is true. If so, 'prompt' is an integer, otherwise it is not.
prompt==round(prompt)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!