字符串在追加

时间:2016-10-19 17:26:16

标签: java

我的任务是创造一个单词猜谜游戏。你只有5次尝试猜测这个词。用户一次输入一个字母以试图找出我的秘密词“果汁”,但你应该使用一个提示词构造,如果它是正确的,每个猜测用正确的字母替换星号。

输出应该是这样的:

  

欢迎使用猜词游戏!你有5次尝试猜测这个秘密词!   目前的提示是

           

猜一个小写字母

     

û

     

* U ***

     

猜猜秘密词

     

tutre

     

继续尝试!

     

猜一个小写字母

     

     

* U ***

     

猜猜秘密词

     

mutor

     

继续尝试!

...继续直到5次尝试然后你要么输赢要么

我的输出不会让你试图猜测,提示词显然搞砸了,因为它有5个星号的空前面。我不知道如何解决它。

  

目前的提示是

     

空*****

     

猜一个小写字母

     

Ĵ

     

jull *

     

猜猜秘密词

     

jutre

     

继续尝试!

     

游戏结束!再试一次?

这是我的课程:

public class SecretWord {
private String secretWord;
private String hintWord;
private int numberOfTurns;

//Default Constructors
public SecretWord()
{
    this.secretWord = "juice";
    for (int i = 0; i < secretWord.length(); i++)
    {
        this.hintWord+="*";
    }
    this.numberOfTurns = 0;
}
//Accessors
public String getSecretWord()
{
    return this.secretWord;
}
public String getHintWord()
{
    return this.hintWord;
}
public int getNumberOfTurns()
{
    return this.numberOfTurns;
}
//Mutators
public void setSecretWord ()
{
    this.secretWord = "juice";
}
public void setHintWord ()
{
    char[] correctLetters = new char[secretWord.length()];
    for (int i = 0; i<secretWord.length();i++)
    {
        hintWord+="*";
        correctLetters[i] += '*';
    }
}
public void setNumberOfTurns (int i)
{
    this.numberOfTurns = 5;
}
//Methods
public void guessLetter(char guess)
{
    String tempHintWord="";
    for (int i = 0; i < secretWord.length(); i++)
    {
        if (secretWord.charAt(i) == guess)
        {
            tempHintWord += guess;
        }
        else
        {
            tempHintWord += hintWord.charAt(i);
        }
    }
    hintWord = tempHintWord;
}

这是我的驱动程序类:

public class SecretWordGame {

//Constant for number of tries
public static final int NUM_TRIES = 5;

public static void main (String[] args)
{
    Scanner keyboard = new Scanner(System.in);

    //Starts game
    boolean quit = false;
    while (quit == false)

    {
        System.out.println("Welcome to the word guessing game! You have " +
                +NUM_TRIES+" tries to guess the secret word!");

                SecretWord myWord = new SecretWord();
        System.out.println("The current hint is \n"+myWord.getHintWord());

        while (myWord.getNumberOfTurns() <NUM_TRIES)
        {
            System.out.println("Guess a lowercase letter");

            //Gets the first letter of what is entered

            char guess = keyboard.nextLine().charAt(0);

            //Updates the hint by calling guess letter method

            myWord.guessLetter(guess);

            System.out.println(myWord.getHintWord());

            System.out.println("Guess the secret word");

            String myGuess = keyboard.nextLine();
            //Checks correct guess
            if (myGuess.equals(myWord.getSecretWord()))
            {
                System.out.println("You win!");
                break;
            }
            else
            {
                System.out.println("Keep trying!");
            }

            myWord.setNumberOfTurns(myWord.getNumberOfTurns()+1);
        }
        //Prompts user to play again

        System.out.println("Game over! Try again?");

        String userInput = keyboard.nextLine();
        if(userInput.equalsIgnoreCase("no"))
        {
            quit = true;
        }
        else
        {
            System.out.println("Let's go again!");
        }
    }
    System.out.println("Goodbye!");
}

2 个答案:

答案 0 :(得分:10)

默认情况下,

SecretWord的{​​{1}}初始化为hintword。因此,当您追加null时,它会将当前值转换为+= "*",然后添加"null"。如果您将hintword初始化,并将其声明为"*",那么它将解决该问题。

答案 1 :(得分:1)

SecretWord课程中,您不会初始化hintWord 因此,在执行hintWord += "*";时,hintWord为null,然后隐式转换为字符串"null"以与"*"连接。*