How to code so that the person can not press enter to continue?

조회 수: 3 (최근 30일)
Kalpha.mc
Kalpha.mc 2020년 11월 17일
편집: Adam Danz 2020년 11월 17일
So i have this for loop with an if statement into it. I have a few questions in my code but you seem to be able to by pass getting the right answer when pressing enter.
How would you fix that ?
This is what i have so far.
pause
clc
for index = 1:1
disp('A = Yellow , B = White , C = Black')
z = input('What Colors Does The Pieces Go On? ','s');
if lower(z) ~= 'c'
fprintf('\n')
disp('False, Start Over!')
fprintf('\n')
disp('The Correct Answer Was Black!')
fprintf('\n')
fd1 = ['Your Final IQ Is ', num2str(md) ,'!'];
disp(fd1)
return
elseif lower(z) == 'c'
fprintf('\n')
disp('Correct!')
fprintf('\n')
md1 = md * 2;
dm1 = ['Your IQ At The Moment Is ', num2str(md1) ,'!'];
disp(dm1)
break
end
end
pause
clc

채택된 답변

Adam Danz
Adam Danz 2020년 11월 17일
편집: Adam Danz 2020년 11월 17일
Create a local function to continually prompt the user as long as the response is empty. The "inputWrapper" function will be added to the bottom of your function/script and you'll need to replace all of the input() commands with the inputWrapper() function using the same inputs you would use with input().
The only way to escape is to enter a non-empty response or by pressing ctrl+c.
out = inputWrapper('Enter something: ');
function t = inputWrapper(varargin)
t = [];
while isempty(t)
t = input(varargin{:});
if isempty(t)
fprintf('You must enter a response.\n')
end
end
end
Note that the input method of acquiring data from the user is highly unconstrained and should be used with input validation to ensure the user entered appropriate responses (see this answer for details).

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by