Borrar filtros
Borrar filtros

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

5 visualizaciones (últimos 30 días)
Sean Murphy
Sean Murphy el 26 de Abr. de 2022
Editada: Walter Roberson el 6 de Jun. de 2022
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!

Respuestas (3)

Image Analyst
Image Analyst el 26 de Abr. de 2022
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 comentarios
Image Analyst
Image Analyst el 26 de Abr. de 2022
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 el 26 de Abr. de 2022
Editada: Walter Roberson el 6 de Jun. de 2022
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.

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 26 de Abr. de 2022
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 el 26 de Abr. de 2022
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 comentarios
Sean Murphy
Sean Murphy el 26 de Abr. de 2022
Thank you, this works great!
Walter Roberson
Walter Roberson el 26 de Abr. de 2022
Does not verify integer.
Do not use eval(), use str2double()

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by