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

5 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Adam Danz
Adam Danz am 17 Nov. 2020
Bearbeitet: Adam Danz am 17 Nov. 2020
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).

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by