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

4 次查看(过去 30 天)
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