-3

所以我正在制作一个刽子手游戏。目前,我正在尝试对用户给出的错误猜测次数进行累加器(在 6 次错误猜测之后,他们输了);但是,我的罢工计数器一直设置为 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

2

阅读如何将值传递给函数。它可以通过值传递或通过引用传递。您的代码按值传递其参数。那就是问题所在。

于 2014-11-17T05:14:54.540 回答
0

我改变了你的代码来工作。

与其传递整数,不如在类内部创建一个私有静态 int 罢工,并提高它的价值。

private static int strikes = 0;

然后不使用罢工计数器,而是将其更改为罢工。

如果这有帮助,请不要忘记接受答案并投票。

于 2014-11-17T05:20:24.803 回答