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
0 个评论
回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!