我正在尝试使用Java制作一个Hangman游戏,但我在互联网上发现的大多数都是有一个while循环,但由于这是一个聊天机器人的一部分,我需要它与if语句,但它不是工作。它正确地抓住第一个单词,发送6个破折号,但每次猜测都是“错误的”,生命就会消失。有谁知道我做错了什么?提前谢谢!
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。 我猜通过guess命令发出的信件。 guess命令的输入被转发给Hangman void。 刽子手检查这封信是否已经猜到了。如果没有,请检查该字母是否在保密字中,并向用户显示猜测的字母。如果它不在秘密词中,则删除生命。如果生命是0,结束游戏。
PS。是的,我知道,我使用的是字符串而不是布尔值,但这是同样的事情,我暂时做了以测试这是不是问题,呵呵。
编辑:这不是同一个问题。在更改了这行代码之后,问题仍然存在。
答案 0 :(得分:2)
代码中的这一行不正确:
if (secret == letter)
它应该是:
if(secret.equals(letter))
因为您正在使用String。