How to verify that a number is an integer from 0-1000?
조회 수: 10 (최근 30일)
이전 댓글 표시
I am trying to verify that an input is an integer from 1-1000 in my code as shown below.
I am also verifying that there are no letters.
When I run this though I can input things like 2.1 or 123.24 and the code will still work and not get caught by the validation.
What function or code can I use to remedy this? I was trying stuff for length(str2num(x)) but that wasn't working for me.
Thank you!
댓글 수: 0
답변 (3개)
Image Analyst
2022년 4월 26일
Here's one way
value = input('Enter an integer between 0 and 1000 : ')
r = rem(value, 1); % Get any fraction part to the right of the decimal point.
if r == 0 && value >= 0 && value <= 1000
fprintf('Input is good.\n');
else
fprintf('Input is out of range or has a fractional part.\n')
end
In r2021b (what I'm using) if you enter letters, it will automatically ask you again without needing to check for letters.
댓글 수: 3
Image Analyst
2022년 4월 26일
Then my code should work perfectly for you as it did for me. There are no red errors whatsoever. The answer you ended up accepting uses the hated eval() which is strongly recommended against. You should not do it that way.
If you still have an error doing it my way, then show me how you altered my code to produce the red errors.
Walter Roberson
2022년 4월 26일
편집: Walter Roberson
2022년 6월 6일
That code might give a yellow mark, warning that you should use semi-colon at the end of the assignment to value to prevent the value from accidentally displaying at the time of the assignment.
value = input('Enter an integer between 0 and 1000 : ');
r = rem(value, 1); % Get any fraction part to the right of the decimal point.
if r == 0 && value >= 0 && value <= 1000
fprintf('Input is good.\n');
else
fprintf('Input is out of range or has a fractional part.\n');
end
Also, be careful about whether exactly 0 is permitted or not.
Walter Roberson
2022년 4월 26일
Trim off leading and trailing whitespace. Verify that the only remaining characters are '0' to '9'. After that take length() of the character representation. If it is 0 then fail. If it is 1 then fail if the string is '0' and otherwise pass. If length is 2 or 3 then pass. If it is 4 then pass if the string is '1000' and fail otherwise. If it is longer, fail.
However this algorithm will not work in the form described in any of these cases:
- you want to permit unary plus, such as +74
- you want to permit trailing decimal point not followed by anything
- you want to permit trailing decimal point followed by 0s
- you want to permit scientific notation.
댓글 수: 0
Jason Shrand
2022년 4월 26일
This should do it:
row_selectionA = input('Your message here: ', 's');
while ~isgood(row_selectionA)
row_selectionA = input('ERROR: (Your error message here)', 's');
end
function result = isgood(s)
if isnan(str2double(s))
result = false;
else
% Result is numeric, so see if it's between 1 and 1000
val = eval(s);
result = val >= 1 && val <= 1000;
end
end
댓글 수: 2
참고 항목
카테고리
Help Center 및 File Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!