How can I keep asking the user for correct input even after they press enter?

8 次查看(过去 30 天)
Hello,
I want my statement to continue to be prompted until correct input is given.
Prompt the user to write "ready" or "Ready" and keep prompting the user until they do so, and if they just pressed enter, a statement shows up asking them to "Please type "Ready"". I am a little lost as to how to do that using while(1) loop. I have this so far...
while (1)
Ready= input('When you are ready to play please type "Ready": ','s');
if Ready== 'ready' | Ready== 'Ready'
break
end
end
Please help
Thank you in advance.

采纳的回答

Adam Danz
Adam Danz 2021-1-29
编辑:Adam Danz 2021-1-29
Most of the time case sensitivity shouldn't be a constraint in these types of queries. The demo below accepts any form of "ready" (READY, ready, Ready, reaDY, etc).
response = '';
while ~strcmpi(response,'ready')
response = input('When you are ready to play please type "Ready": ','s');
end
If you truly only want to accept Ready and ready, use this condition instead,
while ~any(strcmp(response, {'Ready','ready'}))
Another form that gives the user feedback on their error,
instructions = 'When you are ready to play please type "Ready": ';
baseInstructions = instructions;
response = '';
while ~strcmpi(response,'ready')
response = input(instructions,'s');
instructions = sprintf('%s, (you typed "%s") ',baseInstructions, response);
end
Another idea since this is a binary task (the user types the correct word or they do not), why not use a question or message box?
qdh = questdlg('When you are ready to play, please press go!', 'Game', 'Go!','Go!');
You could also add a "Cancel" option.
  5 个评论
Adam Danz
Adam Danz 2021-1-31
I found a copy of the 5th ed of that book and strcmp or strcmpi where not mentioned in any of its 418 pages. I see the author is use R2013a which is really old (maybe the 6th ed has updates). strcmp/strcmpi are the go-to functions for string comparison. ismember is also often used along with lower() or upper() to allow for case insensitivity. But the equality test using "==" is definitely not recommended when comparing character arrays. For strings I guess it's OK but strings weren't even around in R2013a and still, the other functions I mentioned are better. isequal is another option but still not better than strcmp, strcmpi, or ismember.
JkBlack
JkBlack 2021-2-2
编辑:JkBlack 2021-2-2
Your help is very much appreciated! Thank you for pointing this out and sharing this information, will keep it in my mind.
Best Regards,
Jk

请先登录,再进行评论。

更多回答(1 个)

per isakson
per isakson 2021-1-29
Use
strcmpi( Ready, 'ready' )
instead of
Ready== 'ready' | Ready== 'Ready'
Don't use == in combination with character arrays to compare words. It compares character by character.
>> 'abc'=='a*c'
ans =
1×3 logical array
1 0 1
in contrary to strings
>> "abc"=="a*c"
ans =
logical
0
  2 个评论
per isakson
per isakson 2021-1-29
"so I can't use strcmpi" What other function are excluded?
What about this one? Replace
if Ready== 'ready' | Ready== 'Ready'
by
if string(Ready)=="ready" || string(Ready)=="Ready"

请先登录,再进行评论。

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by