我正在开发这个猜谜游戏,用户需要在 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
最佳答案
Solution 1 - You can move
Scanner input = new Scanner(System.in);
to the firstdo
block
do {
//Has moved to the first do block
Scanner input = new Scanner(System.in);
//Rest of your code
}
Solution 2 - You can use
input.next()
insted ofinput.nextLine()
in the seconddo
block
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
}
关于java - 为什么我的 java 猜谜游戏在第一个循环后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59808007/