簡體   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