我正在开发这个猜谜游戏,用户需要在6次尝试中猜出这个单词。他们有能力尝试猜出整个单词,但是如果猜错了,游戏就会结束。游戏结束后,他们可以选择再次玩。我的问题是,当我第二次尝试猜测单词时,仅当我输入n
字符时才给我一个错误。
我稍后将替换添加一个数组而不是静态BRAIN字并将其随机化,但是我想弄清楚。
这里是代码:
/*
* WordGuess.java
*/
import java.util.Scanner;
/**
* Plays a word guessing game with one player.
* */
public class WordGuess {
//Main meathod
public static void main(String[] arqs) {
final String SECRET_WORD = "BRAIN";
final String FLAG = "!";
String wordSoFar = "", updatedWord = "";
String letterGuess, wordGuess = "";
int numGuesses = 0;
String repeat = "";
Scanner input = new Scanner(System.in);
/* begin game */
System. out. println ( "World Guess game. \n" );
do{
numGuesses = 0;
wordSoFar = "";
updatedWord = "";
for (int i = 0; i < SECRET_WORD.length() ; i++) {
wordSoFar += "-"; //word, as dashes
}
System.out.println("Your word is ");
System.out.println(wordSoFar + "\n"); //displays dashes
/* a11ow player to make guesses */
do{
System.out.print("Enter a letter (" + FLAG + " to guess entire world): ");
letterGuess = input.nextLine();
letterGuess = letterGuess.toUpperCase() ;
/* increment number of guesses */
//numGuesses += 1;
/* player correctly guessed a letter--excract string in wordSoFar
* up to the letter guessed and then append guessed. letter to that
* string Next, extract rest of wordSoFar and append after the guessed
* letter
*/
if (SECRET_WORD.indexOf(letterGuess) >= 0) {
updatedWord = wordSoFar.substring(0, SECRET_WORD.indexOf(letterGuess));
updatedWord += letterGuess;
updatedWord += wordSoFar.substring(SECRET_WORD.indexOf(letterGuess)+1,
wordSoFar. length() ) ;
wordSoFar = updatedWord;
}else {
numGuesses += 1;
}
/* display guessed letter instead of dash */
System.out.println(wordSoFar + "\n");
} while (!letterGuess.equals(FLAG) && !wordSoFar.equals(SECRET_WORD) && numGuesses < 6);
/* finish game anil display message anil number of guesses */
if (letterGuess.equals(FLAG)) {
System.out.println("What is your guess? ");
wordGuess = input.nextLine() ;
wordGuess = wordGuess.toUpperCase() ;
}
if (wordGuess.equals(SECRET_WORD) || wordSoFar.equals(SECRET_WORD)) {
System.out.println ( "You won! " ) ;
} else {
System.out.println("Sorry. You 1ose.");
}
System.out.println("The secret word is " + SECRET_WORD);
System.out.println("You made " + numGuesses + " mistake.");
System.out.println("Would you like to play again?");
repeat = input.next();
}while(repeat.equalsIgnoreCase("Y"));
System.out.println("GOOD BYE THANKS FOR PLAYING!");
}
}//end of Word Guess class
用于再次播放的Y
输入被读取为单词的第一个字符。您在完整的输入行中阅读
System.out.print("Enter a letter (" + FLAG + " to guess entire world): ");
letterGuess = input.nextLine(); // <---------- HERE
letterGuess = letterGuess.toUpperCase() ;
但是当您询问他们是否要再次播放时,您只执行input.next()
,这意味着在调用此代码时缓冲区中仍然有一些输入。
如果将input.next()
替换为input.nextLine()
,则这样
System.out.println("You made " + numGuesses + " mistake.");
System.out.println("Would you like to play again?");
repeat = input.next(); // <---------- CHANGE this to repeat = input.nextLine();
然后您的程序将按预期工作。
解决方案1-您可以将
Scanner input = new Scanner(System.in);
移到firstdo
阻止
do {
//Has moved to the first do block
Scanner input = new Scanner(System.in);
//Rest of your code
}
解决方案2-您可以在second
input.next()
块中使用input.nextLine()
插入的do
do { System.out.print("Enter a letter (" + FLAG + " to guess entire world): "); // input.nextLine() Has changed to input.next() letterGuess = input.next(); //.. Rest of code }