필터 지우기
필터 지우기

How to verify that a number is an integer from 0-1000?

조회 수: 6 (최근 30일)
Sean Murphy
Sean Murphy 2022년 4월 26일
편집: Walter Roberson 2022년 6월 6일
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!

답변 (3개)

Image Analyst
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
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
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
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.

Jason Shrand
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
Sean Murphy
Sean Murphy 2022년 4월 26일
Thank you, this works great!
Walter Roberson
Walter Roberson 2022년 4월 26일
Does not verify integer.
Do not use eval(), use str2double()

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by