Java replacement of specific characters

前端 未结 4 898
北海茫月
北海茫月 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条回答
  •  面向向阳花
    2021-01-23 19:06

    Why not have another array of true/false values that mark what letters have been guessed and what letters are still unknown? I don't think using regex and string replaces is an easy way to go about getting hangman to work, especially when they guess another letter.

    Something like this perhaps.

    String answer = "someword";
    boolean[] knownLetters = new boolean[answer.length()];
    for (int i = 0; i < answer.length(); i++) {
        if(knownLetters[i]) {
            System.out.print(answer.charAt(i));
        } else {
            System.out.print("_");
        }
    }
    System.out.println("");
    

提交回复
热议问题