質問

so what i am supposed to be doing, is the user inputs a letter and the program checks to see if the letter is part of the word, from there i cant figure out what i code i need to use to make one of the letters reveal itself heres what i have so far

do {            
            System.out.println("Word: " + secretWord.getWordMask());
            //System.out.print("Guesses: " + guesses);
            System.out.print("Enter your guess: ");
            Scanner keyboard = new Scanner(System.in);
            String guess = keyboard.next();


            WordHider revealChar = new WordHider();
            System.out.println(revealChar.revealLetter(guess));

            //if (secretWord.revealLetter(guess));            

    } while (secretWord.isHiddenWordFound());

and this is another part of a class that i was given in which i have to figure out how to call it a method and implement it

public int revealLetter(String letter) {
    int count = 0;
    String newFoundWord = "";
    if (letter.length() == 1) {
        for (int i = 0; i < secretWord.length(); i++) {
            if ((secretWord.charAt(i) == letter.charAt(0))
                    && (wordMask.charAt(i) == HIDE_CHAR.charAt(0))) {
                count++;
                newFoundWord += letter;
            }
            else {
                newFoundWord += wordMask.charAt(i);
            }
        }
    }
    wordMask = newFoundWord;
    return count;

my main problem is right in the first section where i have WordHider revealChar = new WordHider(); System.out.println(revealChar.revealLetter(guess)); what that is supposed to do is reveal i letter but its definitely not right, any ideas?

役に立ちましたか?

解決

It looks like you're displaying the count instead of wordMask. You could change the return type of revealLetter to String and return wordMask, or if that's not an option, change the code in your do-while to something like this:

revealChar.revealLetter(guess);
System.out.println(revealChar.getWordMask())

他のヒント

Your entire program can be replaced by a couple of lines:

String guessedLetters ="";

Then inside your guessing loop:

guessedLetters += guess;
String display = secretWord.replaceAll("[^" + guessedLetters + "]", "_");
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top