繁体   English   中英

为什么我的 java 猜谜游戏在第一个循环后不起作用

[英]Why does my java guessing game not work after the first loop

我正在开发这个猜谜游戏,用户需要在 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.nextLine()替换input.next()

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); 到第一个do

  do {

        //Has moved to the first do block
        Scanner input = new Scanner(System.in);          

        //Rest of your code

      }

解决方案 2 - 您可以在第二个do块中使用input.next() input.nextLine()

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

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM