If statement with == not working with vector

조회 수: 3 (최근 30일)
Kalista Hall
Kalista Hall 2020년 8월 27일
댓글: Star Strider 2020년 8월 28일
So I'm making a code that's basically Hangman where the user has to guess the letters in a word that's already been decided. I did that by making each word into a vector.
The if/else statement shown below is where, if the letter the user enters matches a letter in the word, it prints "correct" but if it doesn't, it prints "incorrect". My issue is that even if the user's guess matches one of the letter, (and it prints "correct"), it also prints incorrect for every other letter.
if guess==word(k)
disp ('Correct!')
else
disp ('Incorrect')
end
I tried writing it as two seperate if statements but that didn't change anything.
if guess==word(k)
disp ('Correct!')
end
if all(guess~=word(k))
disp ('Incorrect')
end
I also tried the two if statemnts with break at the end of each but that jsut made it print incorrect every time.
Anyone have any idea how to fix this? I'd really appreciate it!
EDIT:
I've added my code as a whole so you can see what I'm trying to do, in case that makes things clearer:
disp ('Lets Play Hangman (the theme is colours)')
disp (' ')
r=randi([1,4],1);
if r==1
word='RED';
elseif r==2
word='ORANGE';
elseif r==3
word='YELLOW';
elseif r==4
word='GREEN';
end
len_word= length(word);
fprintf ('Your letter is %d letters long \n', len_word)
attempts_left=9;
for k=1:len_word
shadow_array(k) = '*';
end
for max_guesses=[1:1:9]
guess=input('Enter a letter: ', 's');
guess=upper(guess);
for k=[1:1:len_word]
word(k);
% PROBLEM AREA
if guess==word(k)
disp ('Correct!')
shadow_array(k)=num2str(word(k));
else
disp ('Incorrect')
attempts_left=attempts_left-1
end
% issue is that it else runs for every letter that ~=k (even if one letter is correct)
end
disp (shadow_array)
if all (shadow_array~='*')
disp ('\nYOU WON!\n')
break
end
end
disp ('GAME OVER')

답변 (1개)

Star Strider
Star Strider 2020년 8월 27일
편집: Star Strider 2020년 8월 27일
To compare strings, use strcmp (or the case-insensitive version, strcmpi, linked to in that page).
Depending on what you want to do, another option is contains.
EDIT —
Some contains examples:
word = 'shark';
guess1 = 'k';
guess2 = 'q';
test1 = contains(word, guess1)
test2 = contains(word, guess2)
.
  댓글 수: 2
Kalista Hall
Kalista Hall 2020년 8월 27일
I tried using contains as shown below but when I did that, if the word does contain the letter, it prints correct 5 (or however many letters are in the word) times and the complete word and jumps straight to the end of the code.
is_it_there=contains(word,guess);
if is_it_there==1
disp ('Correct!')
else
disp ('Incorrect')
end
I've edited my question to add in the code as a whole in case that clarifies anything.
Star Strider
Star Strider 2020년 8월 28일
I do not see that in the other code you posted.
It should not jump to the end of the code. It should instead continue in whatever loop you put it, so only when the conditions for the loop are satisfied should the loop terminate and go to the end of the code. (It would seem that it has to be in the ‘max_guesses’ loop somewhere. I leave it to you to determine where that is.)
Also, experiment with something along the lines of:
if any(it_is_there)
since depending on the word, there could be more than one match, for example ‘YELLOW’ and ‘GREEN’. It is also not necessary to test that it is equal to 1, simply that it is true, and the any function will detect that.

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by