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

조회 수: 12 (최근 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"
JkBlack
JkBlack 2021년 1월 29일
Yes this worked! Thank you very much for sharing this information!
Best Regards,
JK

댓글을 달려면 로그인하십시오.

카테고리

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