How to input letters into an array?

조회 수: 3 (최근 30일)
Kenneth Harp
Kenneth Harp 2017년 3월 14일
댓글: Walter Roberson 2017년 3월 15일
Hello,
I am working on a project for a class I have making a Hangman game. I am trying to input guessed letters into a blank array, but when they are inputted, they show up as the number associated with each letter.
Here is the code I am using:
letters = ismember(word,guess); %letters is a logical array with 1s where the guess, a single letter, is in the word
word_display(letters == 1 ) = guess; %where letters==1, guess is inputted into word display, a zeros array with length of the word.
How do I make it where the word_display displays the letters and not their number that is given them by MATLAB
Thanks!

답변 (1개)

Walter Roberson
Walter Roberson 2017년 3월 14일
Start with
word_display = blanks(length(word));
then at each iteration
[letters, idx] = ismember(word,guess);
word_display(letters) = guess;
  댓글 수: 2
Kenneth Harp
Kenneth Harp 2017년 3월 15일
If I use this method, then I loose my placeholders in my display array. Are there any other methods I am unaware of?
Walter Roberson
Walter Roberson 2017년 3월 15일
Start with
word_display = repmat('_', 1, length(word));

댓글을 달려면 로그인하십시오.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by