Use User Input as a Number to be Checked in If/Else Statement

33 次查看(过去 30 天)
Hello,
I'm having issues figuring this out, even though it seems it should be relatively simple.
I have the user input a number, and that value is then used to determine which cell in a vector is used for a different command.
Based on what I gathered, it seems that User Input is always a string (I could be mistaken), and not considered a numeric value, rather a character.
I have something like this:
prompt = 'Which Day?'
take1 = str2double(input(prompt)) % Take the number the user inputs and use it as a numeric number
if take1==1 || take1==2 || take1>N
% Here I want to check if the Number the user inputs is a certain value
% It can't be 1 or 2 and had to be less than a certain value
disp('Pick Again')
% Then I'll have another prompt for them to choose a different value
elseif -1^(take1*2)==1
% Here I want to make sure the user inputs an integer (no decimals)
% If this is true, I'll use the inputted number
else
disp('Pick Again')
% Another prompt here too
end
I keep on getting error messages, which I think lies in how I'm using the inputted value.
I might end up doing another check to be sure the user does not input another value.
Any help is appreciated.
Also, on a side note, is it possible to have these if/else statement loop back to the initial prompt?
So rather than in each if or elseif section I create a new UI prompt, the script goes back up to the first 'Which day?'.
Thanks.
  1 个评论
Stephen23
Stephen23 2022-12-20
编辑:Stephen23 2022-12-20
" it seems that User Input is always a string (I could be mistaken)"
You are mistaken: if you do not use the 's' option then its output depends much on what the user types. As the documentation explains, the user can also use INPUT() to enter expressions, which could lead to unexpected side effects.
This is why experienced MATLAB users strongly recommend that for robustness and security you should use the 's' option and STR2DOUBLE() if the expected data are scalar numeric:
take1 = str2double(input(prompt,'s'));
Writing better code is simple, when you know how.

请先登录,再进行评论。

采纳的回答

Voss
Voss 2022-12-20
编辑:Voss 2022-12-20
"it seems that User Input is always a string"
The value returned from the input function would be a character vector if you used the 's' option. Without the 's' the returned value will be numeric if the user enters a number.
You are expecting a number, and you are not using 's', which is consistent; you just need to avoid the call to str2double. (str2double with a numeric argument returns NaN.)
"is it possible to have these if/else statement loop back to the initial prompt?"
Yes. Use a while loop.
prompt = 'Which Day?'
while true
take1 = input(prompt); % Take the number the user inputs and use it as a numeric number
if take1==1 || take1==2 || take1>N
% Here I want to check if the Number the user inputs is a certain value
% It can't be 1 or 2 and had to be less than a certain value
disp('Pick Again')
% Then I'll have another prompt for them to choose a different value
elseif rem(take1,1) == 0 % (-1)^(take1*2)==1
% Here I want to make sure the user inputs an integer (no decimals)
% If this is true, I'll use the inputted number
break % exit the while loop
else
disp('Pick Again')
% Another prompt here too
end
end
Note that this considers 0 and negative integers to be valid inputs, and it will throw an error for non-scalar inputs.
  2 个评论
Jon
Jon 2022-12-20
Thank you for your answer to both of my questions.
Also, that's a nicer and neater way to check if it's an integer, probably less expensive too.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by