我对编程和Java还是很陌生(如您所见),并且确实对编程hangman时遇到了一个基本问题。我的代码不是我所知道的最干净的代码,但是我真的很想在我的逻辑上犯错误的地方提供一些建议。我的麻烦是让程序在每次猜测时都能正确输出“ hang子手显示器”。我认为尝试tryWrong ++的方式存在问题。我也无法使重复的字母正常工作,但无法弄清楚。
import java.util.*;
public class HangMan
{
public static void main(String[] args)
{
char[] alphabet =
{
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
};
String[] words =
{
"javascript", "declaration", "object", "class", "failing"
};
int randomIndex = (int) (Math.random() * words.length);
String randomWord = words[randomIndex];
char[] explodedWord = explode(randomWord);
char[] censoredWord = new char[randomWord.length()];
int triesWrong = 0;
Scanner input;
char letterGuess;
boolean letterFound = false;
for (int i = 0; i < randomWord.length(); i++)
{
censoredWord[i] = '_';
}
while (!gameWon(censoredWord, explodedWord))
{
hangMan(triesWrong, censoredWord, alphabet);
System.out.println("Please guess a letter: ");
input = new Scanner(System.in);
letterGuess = input.next().trim().charAt(0);
char[] repeatedLetters = new char[26];
for (int i = 0; i < alphabet.length; i++)
{
if (letterGuess == alphabet[i])
{
if(letterGuess == repeatedLetters[i])
{
System.out.println("You already guessed this letter, please enter another letter.");
}
else
{
repeatedLetters[i] = alphabet[i];
alphabet[i] = '_';
}
}
}
for (int i = 0; i < explodedWord.length; i++)
{
if (letterGuess == explodedWord[i])
{
censoredWord[i] = letterGuess;
letterFound = true;
}
}
if (!letterFound)
{
letterFound = false;
triesWrong++;
}
}
}
public static char[] explode(String bank) //getWord method for string bank
{
//create character array called explode that has length of string
char[] explodedWord = new char[bank.length()];
for (int i = 0; i < bank.length(); i++)
{
explodedWord[i] = bank.charAt(i);
}
return explodedWord;
}
public static void hangMan(int n, char[] dashWord, char[] alph)
{
if (n == 0)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (n == 1)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (n == 2)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("|\t|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (n == 3)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("|\t|\\");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (n == 4)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("| /|\\");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (n == 5)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("| /|\\");
System.out.println("|\t|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
} else if (n == 6)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("| /|\\");
System.out.println("|\t|");
System.out.println("| /");
System.out.println("|");
System.out.println("|");
} else if (n == 7)
{
System.out.println("\n\n---------");
System.out.println("|\t|");
System.out.println("|\t0");
System.out.println("| /|\\");
System.out.println("|\t|");
System.out.println("| / \\");
System.out.println("|");
System.out.println("|");
System.out.println("GAME OVER");
System.exit(0);
}
System.out.println("\n\n");
for (int i = 0; i < alph.length; i++)
{
System.out.print(alph[i] + " ");
}
System.out.println("\n\n");
for (int i = 0; i < dashWord.length; i++)
{
System.out.print(dashWord[i] + " ");
}
System.out.println("\n\n");
}
public static boolean gameWon(char[] dashWord, char[] bigWord)
{
if(dashWord.length != bigWord.length)
{
return false;
}
for(int i = 0; i < dashWord.length; i++)
{
if(dashWord[i] != bigWord[i])
{
return false;
}
}
System.out.println("Congratulations, you win!");
return true;
}
}
最佳答案
if (!letterFound)
{
letterFound = false;
如果
!letterFound
为true,则表示letterFound
已经为false。因此,之后无需将其设置为false。这是一个逻辑缺陷。我建议将
letterFound = false;
移至while循环的开始。因此,您可以在每个循环中重新开始。