This is for a Hangman game. I am using a SortedSet to store the guesses that the user inputs. These letters are stored as char. What I am trying to do is compare each character in a given word to every character in the SortedSet, and append a matched character to a returned String. If it doesn't match, a '-' is appended instead.
For example:
I have a set that contains the guesses: 'a', 'e', 't'
I have a word: apple
The method should return the string "a---e"
Right now, I have a method that generates a String that fits the format I need for one character. I am not sure how to modify it so it works for all characters in a collection.
I don't want to run the entire if/else structure for all of the characters in the collection as that would add too many dashes, so I don't think I can use a for-each loop.
private int generatePattern(String s, char guess) {
String pattern = "";
for (int i = 0; i < length; i++){
if (s.charAt(i) == guess){
pattern += guess + " ";
} else {
pattern += "- ";
}
}
return pattern;
}
Aucun commentaire:
Enregistrer un commentaire