我的累加器重复设置为0?

时间:2014-11-17 18:49:16

标签: java methods accumulator

我正在尝试制作一个刽子手游戏,但我遇到了累积器问题。我老老实实地坚持找到一种方法,累加器不会将自身重置为零,因为我必须在某处初始化它。这是我目前的想法,但我无法弄清楚如何实现它,我想知道你们其中一个人是否会知道。

我在想,在strikesandHangman方法中,我可以使strike仅在第一次调用方法时初始化为0。我不知道如何编码。我不是要求某人为我编写代码,但如果你能指出一些很棒的东西,那我就很困难了。谢谢!

//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);//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){//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
            private int strikeCounter;
            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

3 个答案:

答案 0 :(得分:1)

您可以将int strikes声明为该类的数据成员,因此只有在创建类时才会初始化它:

public class test { //create class
    private static int strikes = 0;

    //your code here...

} //End of class

在这种情况下,private关键字是可选的,但您需要static关键字,因为您想从静态上下文访问strikes变量(您的方法被声明为static

答案 1 :(得分:0)

目前,您正在初始化您的警示计数器变量&#39; strike&#39;作为if语句中的局部变量。你应该把它变成一个类变量,这样就不会在每个方法调用上重新初始化。

答案 2 :(得分:0)

您可以将罢工设为实例变量。然后它只会被初始化一次,你可以做任何事情,而不必将它重置为0.为此,你需要创建一个单独的类,然后在main中创建这个类的实例来使用它。

刽子手:

public class HangMan {
    private int strikes;

    public HangMan () {
        strikes = 0;
    }

    public String letterCheck() {
         //your code here
    }

    public String strikesAndHangman() {
         //your code here
    }
}

测试:

public class Test {
     public static void main(String[] args) {
          HangMan hm = new HangMan();
          //your code, you can call methods from hangman
          String letCheck = hm.letterCheck();
     }
}

同样在Java中,您应该将类​​名称大写,因此测试应命名为Test。