-3

だから私はハングマンゲームを作っています。現時点では、ユーザーが何回間違った推測をしたかを示すアキュムレーターを作成しようとしています (6 回間違った推測をすると、ユーザーは負けます)。ただし、strikeCounter が 0 に設定されたままになり、その理由がわかりません (したがって、ユーザーが何回間違った推測をしても、常に 1 つのストライクがあることが出力されます)。誰かが私のエラーがどこにあるかを確認するのを手伝ってくれますか? ありがとうございました!

//imports:
import java.util.Scanner; //import scanner class
import java.lang.StringBuilder; //Import StringBuider class


public class P3A1_Doron_3918410 { //create class
    public static void main (String[] args){ //create main method
        System.out.println("Hello and welcome to Hangman!"); //Welcome the user
        System.out.println(); //Leave space to organize code
        System.out.println("These are the instructions for the game:"); //Introduce instructions
        System.out.println(); //Leave space to organize code
        System.out.println("You will be tasked with guessing a secret word, which is" +
            " represented by blank underscores. For each round of the game, you will" + 
            " guess a letter. If that letter is in the word, it will appear among the underscores and" + 
            " you continue until you guess the entire word, thus winning the game. If a" +
            " letter is not in the secret word, then you lose that round and gain a" +
            " strike. Once you accumulate more than six strikes, the game is over and you lose.");
        System.out.println(); //Leave space to organize 

        System.out.println("Now let's begin. Here is your secret word:"); //Notify user game will now start
        System.out.println(); //Leave space to organize code

        //create secret word that user sees
        StringBuilder blanks = new StringBuilder("___"); //create initial, blank guessed word
        System.out.println(blanks);//print out hiddenWord's initial, blank state
        System.out.println(); //Leave space to organize code

        //Begin guessing portion of game
        Scanner keyboard = new Scanner(System.in); // Create Scanner object called keyboard
        int strikes = 0;

        for (int i = 0; i < 3; i++){
            while(blanks.charAt(i) == '_'){
                System.out.println(); //Leave space to organize code
                System.out.println("Please guess a letter."); //Ask user to guess a letter
                System.out.println(); //Leave space to organize code
                String letterGuess = keyboard.nextLine(); //Assign letter user guesses to variable letterGuess
                letterCheck(letterGuess, blanks, strikes);//send guess to letterCheck method
                System.out.println(); //Leave space to organize code
                System.out.println(blanks);
            }
        }
        System.out.println(); //Leave space to organize code
        System.out.println("Congratulations! You win!");

    } //End of main method

    public static String letterCheck(String existance, StringBuilder updatedWord, int strikeCounter){//create method that checks whether or not letter exists in secret word
        String secretWord = "cat";
        int index;

        if(secretWord.contains(existance)){
            char letter = existance.charAt(0); //convert string to char
            index = secretWord.indexOf(letter); // return index within this string of the first occurrence of specified char
            updatedWord.setCharAt(index, letter);
            System.out.println(); //Leave space to organize code
            System.out.println("Correct!");
            System.out.println(); //Leave space to organize code
        }   
        else{
            System.out.println(); //Leave space to organize code
            System.out.println("Incorrect.");
            System.out.println(); //Leave room to organize code
            strikeCounter += 1; //add a strike
            System.out.println("You have " + strikeCounter + " strike(s).");
            if(strikeCounter > 5){ //if they have more than 6 strikes
                updatedWord.append("///");
                System.out.println("You have six strikes. Game over.");
            }
            String holdStrikes = Integer.toString(strikeCounter);
            System.out.println(holdStrikes);
            return holdStrikes;
        }
        return updatedWord.toString();
    } //End of letterCheck method

} //End of class
4

2 に答える 2

0

私はあなたのコードを動作するように変更しました。

整数を渡す代わりに、クラス内でその値のすぐ上にプライベート静的 int ストライクを作成します。

private static int strikes = 0;

次に、strikeCounter を使用する代わりに、strikes に変更します。

これが役立つ場合は、回答を受け入れて賛成票を投じることを忘れないでください。

于 2014-11-17T05:20:24.803 に答える