我对此代码有两个问题。当用户猜出字母时,我需要为“船”这个词显示_ _ _ _ _。当用户输入时,需要用猜测的字母替换底空。 我还有一个计数器来跟踪好的猜测,这应该在5次猜测后结束游戏,但是当我运行代码时不会发生这种情况。对这两个问题的任何建议?
private static String [] word = {"b","o", "a","t","s"}; // array to hold the secret word
private static boolean finished = false; // boolean flag to control the main loop
private static int badGuesses = 0; // int variable to hold the number of incorrect guesses
private static ArrayList<String> guessedLetters = new ArrayList<String>(); // String array to mark which letters have been guessed
private static String entryWord = " "; // string variable for the user's guesses
private static int goodGuesses = 0; // int variable to hold the number of correct guesses
private static String guess;
public static void main (String[] args)
{
playGame ();
}
public static void playGame ()
{
displayRules(); // print the instructions to the user
while (!finished)
{
String guess = getGuess(); // allow user to input a guess
checkGuess(word, guess); // identify user's guess as correct or incorrect
showWord (); // print the underscores representing the word
showMan(); // print the appropriate hanging man
}
while (goodGuesses == 5)
winGame();
}
public static void displayRules() // display the rules to the user
{
String userName;
Scanner keyboard = new Scanner (System.in);
System.out.println ("Please enter your name.");
userName = keyboard.next();
System.out.println ("Hello " + userName + "!");
System.out.println ("Welcome to Dan's Hangman game!");
System.out.println ("The objective of the game is to guess all of the letters of the secret word");
System.out.println ("The user will guess a letter and if it is correct, the user has won that round.");
System.out.println ("If the user guesses an incorrect letter, the user will gain a strike.");
System.out.println ("A strike will cause part of the man to be hanged");
System.out.println ("The game will end after the user either gets 6 strikes, or guesses the entire word correctly.");
System.out.println ("----------------------------------------------");
System.out.println(" ");
}
public static void showMan() // display the appropriate hanging man based on the number of incorrect guesses
{
if (badGuesses==0)
man_0();
else if (badGuesses==1)
man_1();
else if(badGuesses==2)
man_2();
else if(badGuesses==3)
man_3();
else if(badGuesses==4)
man_4();
else if(badGuesses==5)
man_5();
else if (badGuesses==6)
man_6();
}
public static void showWord () // Show the status of the word as it is guessed
{
String s = word.toString();
char [] showWordArray = s.toCharArray();
char c = guess.charAt(0);
boolean contains = true; // boolean flag to control the loop
for (int i=0; i<word[0].length(); i++)
{
//showWordArray[i] += '_';
if (showWordArray[i] == c)
{
System.out.print (c);
}
else
System.out.print ('_');
}
System.out.println (" ");
}
public static String getGuess() // allow the user to guess a letter
{
Scanner keyboard = new Scanner (System.in);
System.out.println (" ");
System.out.println ("Please enter a guess");
guess = keyboard.nextLine();
return guess;
}
public static String checkGuess (String [] word, String guess) // identify if the user's guess if correct or incorrect
{
int index = 0; // loop control variable
int element= -1; // element the string is found at
boolean found= false; // flag indicating search results
do
{
if (word[index].equals(guess))
{
System.out.println ("You guessed correctly!");
goodGuesses++;
found = true;
}
index++;
}
while (index< word.length);
if (found == false)
{
System.out.println ("Sorry, you guessed incorrectly ");
badGuesses ++;
}
return Integer.toString (element);
}
public static void loseGame ()
{
if (badGuesses == 6)
System.out.println ("Sorry, you lost! :(");
finished = true;
}
public static void winGame()
{
if (goodGuesses == 5)
System.out.println ("Congratulations! You have won!");
finished = true;
}
public static void man_0()
{
System.out.println ("_____");
System.out.println ("| |");
System.out.println ("|");
System.out.println ("|");
System.out.println ("|");
System.out.println ("You have 6 guesses left");
}
public static void man_1()
{
System.out.println ("_____");
System.out.println ("| |");
System.out.println ("| o");
System.out.println ("|");
System.out.println ("|");
System.out.println ("You have 5 guesses left");
}
public static void man_2()
{
System.out.println ("_____");
System.out.println ("| |");
System.out.println ("| o");
System.out.println ("| |");
System.out.println ("|");
System.out.println ("You have 4 guesses left");
}
public static void man_3()
{
System.out.println ("_____");
System.out.println ("| |");
System.out.println ("| o");
System.out.println ("| /|");
System.out.println ("|");
System.out.println ("You have 3 guesses left");
}
public static void man_4()
{
System.out.println ("_____");
System.out.println ("| |");
System.out.println ("| o");
System.out.println ("| /|\\");
System.out.println ("|");
System.out.println ("You have 2 guesses left");
}
public static void man_5()
{
System.out.println ("_____");
System.out.println ("| |");
System.out.println ("| o");
System.out.println ("| /|\\");
System.out.println ("| /");
System.out.println ("You have 1 guess left");
}
public static void man_6()
{
System.out.println ("_____");
System.out.println ("| |");
System.out.println ("| o");
System.out.println ("| /|\\");
System.out.println ("| / \\");
loseGame();
}
}
答案 0 :(得分:0)
基本上,showWord
方法的逻辑是错误的。但不是为你纠正,而是以问题的形式给你一个提示......
要注意的另一件事是,word
作为String
对象数组的表示是&#34;不自然&#34;。自然表示为String
,char[]
是可能的替代方案。
我之所以提到这一点,主要是因为您当前选择的表示方式使您的代码比您需要的更复杂。