所以我有这个项目,我目前陷入代码的“guessLetter”部分。
public boolean guessLetter( String letter ) {
/* If the letter has already been guessed, print out a message that
* it's already been guessed and return true.
*/
letter = letter.toUpperCase();
for(int x = 0; x<letter.length(); x++){
String a = letter.substring(x, x+1);
if(letter.equals(a)){
System.out.println("This letter was already found");
}
}
/* Check to see if the letter is in phrase. If it is, put it into
* solved in the correct place, replacing the underscore.
*/
for(int y = 0; y<letter.length(); y++){
String blank = " ";
String b = letter.substring(y, y+1);
return true;
}
/* Fix this so it returns the right value, every time */
return false;
}
你可以看到我一直在尝试的东西,但我认为我现在的代码根本不起作用,很明显我还没有完成。
如果你能解释我的错误,为什么,我会非常感激。
我喜欢做作业:-)
这应该让你开始。
public class HangMan {
HashSet<String> guessedLetters=new HashSet();
String phrase="myWord";
public boolean guessLetter(String letter){
if(guessedLetters.contains(letter)){
return true;
}else{
guessedLetters.add(letter);
return false;
}
}
public void inPhrase(String letter){
if(phrase.contains(letter)){
//Get all index of letter
int index = phrase.indexOf(letter);
while (index >= 0) {
index = phrase.indexOf(letter, index + 1);
//Show the letter at this index
}
}else{
//Decrease the number of guesses till zero, then update hangman?
}
}
}