do {
System.out.println("Word: " + secretWord.getWordMask());
//System.out.print("Guesses: " + guesses);
System.out.print("Enter your guess: ");
Scanner keyboard = new Scanner(System.in);
String guess = keyboard.next();
WordHider revealChar = new WordHider();
revealChar.revealLetter(guess);
System.out.println(revealChar.getWordMask());
secretWord.revealLetter(guess);
if (guess != ???) {
System.out.println("Miss");
}
所以我有那么多,它做了什么它要求用户猜一个字母,但我不知道如何使它如此,如果他们猜的字母是正确的,打印一个“未命中”的消息。 / p>
public int revealLetter(String letter) {
int count = 0;
String newFoundWord = "";
if (letter.length() == 1) {
for (int i = 0; i < secretWord.length(); i++) {
if ((secretWord.charAt(i) == letter.charAt(0))
&& (wordMask.charAt(i) == HIDE_CHAR.charAt(0))) {
count++;
newFoundWord += letter;
}
else {
newFoundWord += wordMask.charAt(i);
}
}
}
wordMask = newFoundWord;
return count;
上面的代码是显示该字母的代码,如果猜到的话。
答案 0 :(得分:2)
您的secretWord.revealLetter(guess)
已经count
返回guess
secretWord
在if
中的次数,当它是未命中时为0。所以你可以把它放在if (secretWord.revealLetter(guess) == 0) {
System.out.println("Miss");
}
条件:
secretWord.revealLetter(guess)
您可以在if
之前删除对{{1}}的来电,因为您只需要拨打一次。