現在好了,在課堂上,我們被分配另一個劊子手項目與數組的使用,我們不得不產生「_」的。 當用戶輸入了「wordAnswer」我已經成功地生成所有的空白。 我的問題是,我無法弄清楚如何與空白代替正確的猜測字符「_」。劊子手第2部分使用數組的Java
這裏是我的整個程序代碼,我知道它不是全部齊全。
import cs1.Keyboard;
public class Game {
public static void main(String[] args) {
int correctGuess = 0;
int wrongGuess = 0;
int bodyParts = 0;
char guess;
boolean foundIt;
System.out.println("Welcome to Hangman!");
System.out.println("Please enter a word for the other user to guess!");
String wordAnswer = Keyboard.readString();
char[] chars = wordAnswer.toCharArray();
char[] blanks = new char[wordAnswer.length()];
System.out.println("You have 6 attempts to guess the word!");
do {
for (int i = 0; i < wordAnswer.length(); i++) {
System.out.print(" _ ");
}
System.out.println("Please enter in your guess!");
guess = Keyboard.readChar();
for (int i = 0; i < chars.length; i++) {
if (guess == chars[i]) {
blanks[i] = guess;
foundIt = true;
}
}
} while (correctGuess < wordAnswer.length() && wrongGuess != 6); {
System.out.println("Congratulations! You have solved the word!");
}
}
}
我知道,你必須使用一個for循環才能夠空白更改爲正確的猜測字母,但是當你運行該程序,並在正確的猜測進入空白不會變成正確的信。 這是我的OutPut
Welcome to Hangman!
Please enter a word for the other user to guess!
program
You have 6 attempts to guess the word!
_ _ _ _ _ _ _ Please enter in your guess!
p
_ _ _ _ _ _ _ Please enter in your guess!
r
_ _ _ _ _ _ _ Please enter in your guess!
o
_ _ _ _ _ _ _ Please enter in your guess!
g
_ _ _ _ _ _ _ Please enter in your guess!
r
_ _ _ _ _ _ _ Please enter in your guess!
a
_ _ _ _ _ _ _ Please enter in your guess!
m
_ _ _ _ _ _ _ Please enter in your guess!
我期待的是!
Welcome to Hangman!
Please enter a word for the other user to guess!
program
You have 6 attempts to guess the word!
_ _ _ _ _ _ _ Please enter in your guess!
p
p _ _ _ _ _ _ Please enter in your guess!
r
p r _ _ _ _ _ Please enter in your guess!
o
p r o _ _ _ _ Please enter in your guess!
等等
這是我,但提示和幫助將是非常感激!
System.out.println("Please enter in your guess!");
guess = Keyboard.readChar();
for (int i = 0; i < chars.length; i++) {
if (guess == chars[i]) {
blanks[i] = guess;
foundIt = true;
}
}
「不工作」 是不是一個有用的問題說明。 *它是如何「不工作」?編譯錯誤?運行時異常?什麼是錯誤/異常?您是否通過Google努力瞭解它是什麼以及它發生的原因?或者這是不正確的行爲?發生了什麼?你期望會發生什麼?更詳細。 – tnw 2014-10-28 19:56:49
還不是很清楚。你期望看到第二行的'_ _ _ _ _ _ _ _',第三行的'p _ _ _ _ _'等嗎? – Andrew 2014-10-28 20:03:47
好吧,我已經更新了它,我不認爲我真的可以更具體。我正在尋找的是正確的猜測字母來代替「_」空白。 – 2014-10-28 20:03:49