追加后返回null的字符串 [英] string returning null on append

查看:45
本文介绍了追加后返回null的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我受命创建一个猜词游戏.您只有5次尝试猜单词的机会.用户一次输入一个字母试图找出我的秘密单词"juice",但是您应该使用提示单词结构,如果正确,每个猜测都会用正确的字母替换星号.

I've been tasked to create a word guessing game. You only have 5 tries to guess the word. User enters in one letter at a time to try to figure out my secret word which is "juice" but you're supposed to work with a hint word construct which each guess replaces the asterisks with the correct letter if it is correct.

输出应该是这样的:

欢迎来到猜词游戏!您有5次尝试猜出这个秘密单词的机会!当前提示是

Welcome to the word guessing game! You have 5 tries to guess the secret word! The current hint is

猜一个小写字母

u

* u ***

猜个秘密

tutre

继续尝试!

猜一个小写字母

t

* u ***

猜一个秘密的词

变种

继续尝试!

...持续进行5次尝试,然后您获胜或失败

... Continues on till 5 tries then you either win or lose

我的输出不会一直让您尝试猜测,并且提示词的前5个星号都为空,因此提示词显然被弄乱了.我不确定如何解决它.

当前提示是

null *****

null*****

猜一个小写字母

j

jull *

猜个秘密

黄麻

继续尝试!

游戏结束!再试一次?

这是我的课程:

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!");
}

推荐答案

SecretWord 提示字词默认情况下初始化为 null .因此,当您将其附加到 + ="*" 时,会将当前值转换为"null" ,然后添加"*" .如果您在将提示词声明为" 的地方初始化提示词,它将解决此问题.

SecretWord's hintword is initialized to null by default. So when you append to it += "*" it converts the current value to "null" and then adds the "*". If you initialize hintword where you declare it to "" it will fix that issue.

这篇关于追加后返回null的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆