strings, while loop help
Ältere Kommentare anzeigen
clear; clc;
fid = fopen('hangman.txt','r');
if fid < 0, error('Cannot open file');
end
data = textscan(fid,'%s');
data = data{1};
fclose(fid);
index = ceil(rand * numel(data));
word = data{index};
masked = word;
masked(~isspace(masked)) = '*';
complete = 0;
while complete == 0
clc; fprintf('Word : %s\n',masked);
letter = (input('Guess a letter : ','s'));
stat = findstr(word,letter);
if ~isempty(stat)
masked(stat) = letter;
end
if isempty(findstr(masked,'*'))
complete = 1;
end
end
clc; fprintf('Word : %s\n',masked);
My letters are still case sensitive, not sure why. And I'm not sure how I could break the while loop so it stops after six incorrect guesses.
1 Kommentar
Walter Roberson
am 5 Dez. 2011
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Please remember that if you write your questions in a format that is difficult for the volunteers to understand, they might choose to skip the question rather than puzzling it out. It is thus to your advantage to spend time learning how to format code.
Akzeptierte Antwort
Weitere Antworten (1)
Sean de Wolski
am 5 Dez. 2011
you can use lower and upper to force strings to your choice of capitalization.
doc lower
doc upper
Kategorien
Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!