代码将累加器设置为0?

时间:2014-11-17 05:09:04

标签: java hang man accumulator

所以我正在制作一个刽子手游戏。此刻,我正在努力为用户提供多少错误的猜测做出累加器(经过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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

我将您的代码更改为有效。

不是传递整数,而是在类中创建一个私有静态int strikes 只是它的价值。

private static int strikes = 0;

然后,不要使用strikeCounter,而是将其更改为strikes。

如果这有助于不要忘记接受回答和upvote。