Hangman Java - 替换下划线

时间:2015-10-17 04:53:27

标签: java replace

我几乎完成了我的Hangman游戏,但是我有一个问题就是更换正确猜到的字母'用这些字母下划线。每当我选择正确的字母时,都会创建一个无限循环。非常感谢任何帮助。

// public String phrase;

// public String newMask;

public boolean showLetter(String letter)
{
    phrase = phrase.toUpperCase();

    int pos = phrase.indexOf(letter);
    if(pos != -1) { //If the letter is part of the phrase.
        do {
            //Letter and phrase don't change.
            //Cut the "a" 

            pos = phrase.indexOf(letter);
            //Make a new string and take the digit out. 
            //I must break out of the loop.
            //add code here 

            newMask = phrase.substring(0,pos)+phrase.charAt(pos)+phrase.substring(pos);

        } while (pos != -1);
        setValue(mask);
    } else {
        return false;
    }
    return true;
}

1 个答案:

答案 0 :(得分:0)

嘿,汤米我做了类似的方法,当你输入字母时,它将检查它是否在短语String中,如果它在其中,它将返回true,并打印出新的掩码。

Id

输入:“w”

输出:True,“w _ _ _”

如果您不想在System.out.println(mask)前面输出“//”输出;