how do i make the user only input 1 letter at a time with this code?

20 次查看(过去 30 天)
play = menu("Play A Word Game? ","Yes","No");
cPLAY = 1;
while play == 0 && cPLAY < 5
play = menu("Play A Word Game? ","Yes","No");
cPLAY = CPLAY + 1;
end
if play == 0
error('TOO LATE')
end
while play == 1
fprintf("Guess the 5 letter word: _____ \n\n")
index = randi(495); %the bank of words I am using has 496 words
wordstring= string(WORDS(index));
WORD = char(WORDS(index)); % must use a character array,
fprintf('A word has been chosen. Please click to continue.')
pause()
% Setup Correct and Wrong character strings (Task 2)
wrong = 0;
right = 0;
while right < 5 && wrong < 9
clc
correct = char("_____");
badGuesses = '';
disp(correct);
fprintf("\n----- %d of 9 incorrect guesses: %s ------\n\n", wrong, string(badGuesses));
GUESS = input("What is your guess? Please enter lowercase letters only: \n", 's');
GUESS = char(GUESS);
% Check if the guess is correct or not (Task 3)
anyRight = 0
for x = 1:1:5
if WORD(x) == GUESS
correct(x) = GUESS;
anyRight = 1;
right = right + 1;
end
end
if anyRight == 0
badGuesses = [badGuesses,GUESS];
wrong = wrong + 1;
end
end

回答(1 个)

Walter Roberson
Walter Roberson 2024-11-1,19:32
移动:Walter Roberson 2024-11-1,19:32
input() cannot be constrained to accept only single characters.
You could menu() or listdlg() listing the possible characters. As an enhancement, your list of possible characters could be changed to eliminate the characters that have already been chosen.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by