Java replacement of specific characters

2019-02-28 06:13发布

问题:

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 word "ghost" and ghost is replaced with "_ ",

hiddenWord = ghost.length();
for (i=0; i < ghost.lenth(); i ++)
System.out.print("_ ")

giving us an output of

"_ _ _ _ _ "

Lets say we guess the letter "O" the letter "o" is guessed, how do i replace `

"_ _ _ _ _" with 
"_ _ o _ _ "

my current class file

public void pickWord()
    {
        String[] listOfWords;
        listOfWords = new String[10];
        listOfWords[0] = "shenanigans";
        listOfWords[1] = "conversely";
        listOfWords[2] = "octopus";
        listOfWords[3] = "dizzy";
        listOfWords[4] = "malicious";
        listOfWords[5] = "goosebumps";
        listOfWords[6] = "flying";
        listOfWords[7] = "staff";
        listOfWords[8] = "xylophone";
        listOfWords[9] = "zapping";
        Random generator = new Random();
        int lineNumber = generator.nextInt(9);
        disguisedWord = listOfWords[lineNumber];


    }   
    public void displayMark()
    {
        for( int i = 0; i < disguisedWord.length(); i ++)
            underscore = underscore + "_ ";
        System.out.println(underscore);

    }
    public void makeGuess() throws IOException
    {
        System.out.println("Your word is " + disguisedWord.length() + " letters long.");
        System.out.println("Feel free to guess a letter.");
        guess = (char)System.in.read();

回答1:

String ghost = "ghost";
String input = "o";
for (int i = 0; i < ghost.length(); i++) {
    if (String.valueOf(ghost.charAt(i)).equalsIgnoreCase(input)) {
        System.out.print(input + " ");
    } else {
        System.out.print("_ ");
    }
}

You can simplify the if statement using the ternary operator:

System.out.print(String.valueOf(ghost.charAt(i)).equalsIgnoreCase(input) ? input + " " : "_ ");


回答2:

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("");


回答3:

The easiest way to do that would be to use a Java regular expression on the target word. So whenever a user types in a letter you look for that letter in a regex match. That match will give you back the index of matching characters which you can then use to replace the letter in the __ version of the string.

...    
    listOfWords[9] = "zapping";
    Random generator = new Random();
    int lineNumber = generator.nextInt(9);
    disguisedWord = listOfWords[lineNumber];
    char[] hidden = disguisedWord.replaceAll("*","_").toCharArray();
}

public void makeGuess() throws IOException {
    System.out.println("Your word is " + disguisedWord.length() + " letters long.");
    System.out.println("Feel free to guess a letter.");
    String guess = System.in.read();

    Pattern pat = new Pattern(guess);
    Matcher mat = pat.matcher(disguisedWord);
    while (mat.find()) {
        int start = mat.start();
        hidden[start] = guess.toCharArray()[0];
    }
}


回答4:

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".