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

11 ビュー (過去 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 Center および File ExchangeScope Variables and Generate Names についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by