Let users enter integer, warn them if they enter anything else

20 次查看(过去 30 天)
I want users to enter an integer. If they enter anything else I want to display a warning message. This is my best attempt so far
input_hf = input('Enter an integer: ');
if ~isnumeric(input_hf) || ~fix(input_hf) == input_hf
disp('Please enter an integer')
end
But it doesn't work. When users enter a string the application crashes. Here are the results from a failed test run:
Enter an integer: a
??? Error using ==> input
Undefined function or variable 'a'.
Error in ==> skapaPlot at 4
input_hf = input('Enter an integer: ');
How can I change my code to make it work as intended?
Thanks

回答(3 个)

Sean de Wolski
Sean de Wolski 2011-5-11
input_hf = str2double(input('Enter an integer: ','s'));
if isnan(input_hf) || fix(input_hf) ~= input_hf
disp('Please enter an integer')
end

Andy
Andy 2011-5-11
From the documentation of input: "The response to the input prompt can be any MATLAB expression, which is evaluated using the variables in the current workspace." When you put in a, MATLAB tries to evaluate it. If you just want to return the string the user inputs, then you need to use the second argument 's' of input (again, see the documentation). Only then, as Sean de pointed out, should you try to convert to a double. At this point, if the input was not numeric, str2double returns NaN, so you can alter your conditional check (and I'll throw it in a while loop as well, so that it asks more persistently) to get:
input_hf = str2double(input('Enter an integer: ', 's'));
while isnan(input_hf) || fix(input_hf) ~= input_hf
input_hf = str2double(input('Please enter and INTEGER: ', 's'));
end
  2 个评论
Md. Abdullah AL Mamun
But this don't take more than two string. If I input 2 string, first time it says 'Please enter an integer'.
But next time it executes the file without any value 'NaN'. How to make a return loop?
Jan
Jan 2018-5-15
编辑:Jan 2018-5-15
@Abdullah AL Shohag: Please use flags only to inform admins and editors about contributions, which might conflict with the terms of use, e.g. by rudeness or spam.
You cannot input 2 strings by one input command. You can enter one string containing 2 numbers.
To accept more numbers:
num = NaN;
while any(isnan(num(:))) || ~all(fix(num(:)) ~= num(:)) ...
|| (numel(num) ~= 2)
s = input('Please enter two INTEGERS: ', 's');
s = strrep(',', ' ');
num = sscanf(s, '%g %g');
end

请先登录,再进行评论。


Matt Fig
Matt Fig 2011-5-11
a is not a string. In MATLAB a string has a single quote. This is how you enter a string:
Enter an integer: 'a'
When you just type a, MATLAB looks for a variable named a, as in:
a = 5; % Now a is defined!
Enter an integer: a
If the user types just a with no quotes, that is a user mistake and has nothing to do with your code. It is like doing this:
tan(b) % Without defining b first, this will error....
This understanding lets the user enter a pre-defined integer as above (just like with the call to TAN). Also, the user can do like:
Enter an integer: floor(pi)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by