问题
I'm doing this hangman project, and I'm almost done, but I can't get the output to show all of the correct letters in place of the underscore, instead it only shows the correct letter that was just entered. Please help.
import java.util.Random;
public class HangManGames
{
public static int i;
public static int errors = 0;
public static String wordList[] = {"alabama","alaska","arizona","arkansas","california","colorado","connecticut","delaware","florida","georgia","hawaii","idaho","illinois","indiana","iowa","kansas","kentucky","louisiana","maine","maryland","massachusetts","michigan","minnesota","mississippi","missouri","montana","nebraska","nevada","new hampshire","new jersey","new mexico","new york","north carolina","north dakota","ohio","oklahoma","oregon","pennsylvania","rhode island","south carolina","south dakota","tennessee","texas","utah","vermont","virginia","washington","west virginia","wisconsin","wyoming"};
public static boolean guessLetter;
public static String mysteryWord;
public static int mysteryWordLength;
public static int remainingLetters;
StringBuffer buffer = new StringBuffer();
public static void Start()
{
System.out.println(" _______ ");
System.out.println(" | |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("_____|_____ ");
}
public static void Error1()
{
System.out.println(" _______ ");
System.out.println(" | |");
System.out.println(" | ( )");
System.out.println(" | ");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("_____|_____ ");
}
public static void Error2()
{
System.out.println(" _______ ");
System.out.println(" | |");
System.out.println(" | ( )");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" |");
System.out.println("_____|_____ ");
}
public static void Error3()
{
System.out.println(" _______ ");
System.out.println(" | |");
System.out.println(" | ( )");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | /");
System.out.println("_____|_____ ");
}
public static void Error4()
{
System.out.println(" _______ ");
System.out.println(" | |");
System.out.println(" | ( )");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | / \\");
System.out.println("_____|_____ ");
}
public static void Error5()
{
System.out.println(" _______ ");
System.out.println(" | |");
System.out.println(" | ( )");
System.out.println(" | |");
System.out.println(" | /|");
System.out.println(" | |");
System.out.println(" | / \\");
System.out.println("_____|_____ ");
}
public static void Error6()
{
System.out.println(" _______ ");
System.out.println(" | |");
System.out.println(" | ( )");
System.out.println(" | |");
System.out.println(" | /|\\");
System.out.println(" | |");
System.out.println(" | / \\");
System.out.println("_____|_____ ");
}
public static void main(String args[])
{
do{
Random random = new Random();
int index = random.nextInt(50);
mysteryWord = wordList[index];
mysteryWordLength = mysteryWord.length();
remainingLetters = mysteryWordLength;
String result = "";
for (i = 0; i < mysteryWordLength; i++)
result += "_ ";
System.out.println(result);
System.out.println(" ");
while (remainingLetters != 0 && errors < 6) {
if (errors == 0){
System.out.println("");
Start();
System.out.println("");}
else if (errors == 1){
System.out.println("");
Error1();
System.out.println("");}
else if (errors == 2){
System.out.println("");
Error2();
System.out.println("");}
else if (errors == 3){
System.out.println("");
Error3();
System.out.println("");}
else if (errors == 4){
System.out.println("");
Error4();
System.out.println("");}
else if (errors == 5){
System.out.println("");
Error5();
System.out.println("");}
else if (errors == 6){
System.out.println("");
Error6();
System.out.println("");}
System.out.print("Guess a letter:");
char guess = Expo.enterChar();
guessLetter = (mysteryWord.indexOf(guess)) != -1;
if (guessLetter == false) {
System.out.println("Sorry, bad guess");
errors++;
System.out.print(result);
} else {
System.out.println("Great guess!");
for (i = 0; i< mysteryWordLength; i++) {
if (mysteryWord.charAt(i) == guess)
System.out.print(guess);
else {
remainingLetters--;
System.out.print("_");
System.out.print(result.charAt(i));
}
}
}
}
}while(remainingLetters != 0 && errors < 6);
System.out.println();
System.out.println();
if (remainingLetters == 0 && errors <6)
{
System.out.print("YAY! You Win!");
System.out.println("The word was " + mysteryWord);
}else
{
System.out.println("Game Over! You Lose!");
System.out.println("The word was " + mysteryWord);
}
}
}
回答1:
You should make a variable called lettersGuessed
that is either initially an empty string, ""
, or an ArrayList<Character>.
Then after each guess, or this line char guess = Expo.enterChar();
add the char guess
to your ArrayList or String (I suggest a string if you are unfamiliar with an arraylist) so lettersGuessed += guess;
and then System.out.println("Letters guessed: " + lettersGuessed)
.
来源:https://stackoverflow.com/questions/37423159/java-hangman-game