我想用Java做一個Hang子手遊戲,但是我在互聯網上發現的大部分都是用while循環,但是因爲這是chatbot的一部分,所以我需要它與if語句,但它不起作用。它正確地抓住第一個單詞,發送6個短劃線,但是每個猜測都是「錯誤的」,並且生活會消失。有誰知道我做錯了什麼?提前致謝!爪哇試圖製作Hang子手
private static String hangmanStarted = "0";
private static String hangmanInitialized = "0";
private static String hangmanSecret = "";
private static int hangmanLives = 10;
private void cmdHangman() {
if (hangmanStarted.equals("0")) {
try {
secretWord();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
hangmanStarted = "1";
sendMessage("Hangman has started!\nType: ''.guess letter''\nto guess a letter!");
}
}
private void cmdGuess() {
if (hangmanStarted.equals("1")) {
String input = lastMsg.replace("[^a-zA-Z]", "");
input = input.replace("guess", "");
input = input.toLowerCase();
try {
Hangman(input);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void Hangman(String input) throws IOException {
StringBuilder guessedLetters = new StringBuilder();
String dashes = secretDashes();
if (guessedLetters.toString().contains(input)) {
sendMessage("This word is already guessed");
} else {
if (hangmanSecret.contains(input)) {
guessedLetters.append(input);
if (hangmanSecret.equals(letterConversion(hangmanSecret, secretDashes(), input))) {
sendMessage("You win!");
hangmanInitialized = "0";
hangmanStarted = "0";
guessedLetters.delete(0, guessedLetters.length());
} else {
guessedLetters.append(input);
sendMessage("Letter was found!\nWord: " + letterConversion(hangmanSecret, secretDashes(), input));
}
} else {
guessedLetters.append(input);
hangmanLives = hangmanLives - 1;
sendMessage("Word: " + dashes + "\nLives: " + hangmanLives);
if (hangmanLives == 0) {
sendMessage("GAME OVER: You are dead.");
hangmanInitialized = "0";
hangmanStarted = "0";
hangmanLives = 10;
guessedLetters.delete(0, guessedLetters.length());
}
}
}
}
public static void secretWord() throws FileNotFoundException {
if (hangmanInitialized.equals("0")) {
hangmanInitialized = "1";
Scanner infile = new Scanner(new FileReader("hangWords.txt"));
hangmanSecret = infile.next();
infile.close();
}
}
public static String secretDashes() throws FileNotFoundException {
StringBuilder dashes = new StringBuilder();
for (int count = 0; count < hangmanSecret.length(); count++) {
dashes.append('-');
}
return dashes.toString();
}
public static String letterConversion(String secret, String dashes, String letter) {
StringBuilder conversion = new StringBuilder();
for (int index = 0; index < secret.length(); index++) {
char secretChar = secret.charAt(index);
secret = String.valueOf(secretChar);
char letterChar = letter.charAt(index);
if (secret.equals(letter))
conversion.setCharAt(index, letterChar);
dashes = conversion.toString();
}
return dashes;
}
它應該做的:
我開始劊子手用命令。 我猜通過猜測命令的字母。 猜測命令的輸入被轉發給Hangman void。 Hang子手檢查是否已經猜到了這封信。如果沒有,請檢查該字母是否在祕密字詞中,並向用戶顯示猜出的字母。如果它不在祕密詞中,請移除生命。如果生命是0,結束遊戲。
PS。是的,我知道,我使用的是字符串而不是布爾值,但它是一樣的,我暫時用它來測試這是否是問題,嘿。
編輯:這不是同一個問題。改變了這一行代碼後,問題仍然存在。
那麼,爲什麼我會下降?我只展示了必要的代碼,解釋了問題並解釋了它應該做的事情。我沒有加上感謝或問候。我遵循所有規則。我認爲這不值得投票。 –
在你的letterConversion()方法改變了祕密和字母的比較 –
@KevinEsche這並沒有解決它,仍然是同樣的問題! :(感謝您的輸入,儘管如此。 –