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

标签 java substring indexof string-length

我正在开发这个猜谜游戏,用户需要在 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 first do 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 of input.nextLine() in the second do 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/

相关文章:

java - 如何在 gradle 中创建 zip 时创建空文件夹

java - Jboss 5CR2 支持 EJB2

C:评估字符串的一部分

Java 字符串函数

c# - .NET 字符串 IndexOf 意外结果

list - DART:实例列表中的 indexOf()

javascript - 在触摸屏上添加声音

java - 从 XML 文档、XSLT 和 JAXB 中删除元素

java - 在 Fedora 25 上使用 React Native for Android 时遇到的问题

regex - 在 PostgreSQL 中使用子字符串获取 JSON 值