Java replacement of specific characters

前端 未结 4 893
北海茫月
北海茫月 2021-01-23 18:32

This is my first question on this site so i\'ll try not to be a total noob..

I\'m currently creating hangman game in java. So my question to you is if we are given a wor

4条回答
  •  -上瘾入骨i
    2021-01-23 18:52

    I think the most easy way would be storing the state in a variable and by looping over the real world replace the matching characters.

    private String word; 
    private char[] state;
    
    public void guess(char input){
        for (int i = 0; i < word.length(); i++) {
            if (word.toLowerCase().charAt(i) == input){
                state[i] = word.charAt(i);
            }
        }
    }
    

    afterwards you can print the contents of "state".

提交回复
热议问题