artshof
artshof

Reputation: 13

Avoid resetting integer variable

I'm making a game in Java where you have to take care of a dog. I made my game method call itself so that I won't have to copy and paste the contents in this method several times. The problem is that I don't know how to go around the two integer values I declared, because the integer values are added and subtracted through each choice and calling the method again will change those changed values back to default.

    import java.io.File;
    import java.util.Scanner;

    public class Main {

        public static Scanner kybd = new Scanner(System.in);

        public static void main(String[] args) {
            game();
        }

        public static void game() {
            Integer diet;
            diet = 5;
            Integer happiness;
            happiness = 10;
            System.out.println("");
            System.out.println("Dog \t Hunger: " + diet 
                               + "\n \t Happiness: " +        happiness);
            System.out.println("");
            System.out.println("1. Feed \n2. Play \n3. Ignore");
            System.out.println("");
            System.out.print("> ");
            String input = kybd.nextLine();
            if (input.equalsIgnoreCase("1")) {
                diet++;
                game(); // This is supposed to go to the 
                        // beginning with the changed value of diet.
            } else if (input.equalsIgnoreCase("2")) {
                happiness++;
                game(); // This is supposed to go to the 
                        // beginning with the changed value of happiness.
            } else if (input.equalsIgnoreCase("3")) {
                happiness--;
                diet--;
                game(); // This is supposed to go to the
                        // beginning with the changed value of happiness.
            } else {
                System.out.println("Invalid Input");
                game(); // This is supposed to go the beginning
                        // where you can change your input but 
                        // still has your changed values.
            }
    if (diet <= 0);
    {
        System.out.println("Your dog died because it did not eat.");
        game(); // This is supposed to go to the beginning 
                // with the default values.
    }
    if (diet > 10);
    {
        System.out.println("Your dog died because it was overfed.");
        game(); // This is supposed to go to the 
                // beginning with the default values.
    }
    if (happiness <= 0);
    {
        diet--;
        System.out.println("Your dog is no longer happy. He will not eat.");
    }
    {
        if (happiness > 10);
        System.out.println("Your dog died because it was too excited.");
        game(); // This is supposed to go to the 
                // beginning with the default values.
    }
}
}

Upvotes: 1

Views: 1094

Answers (1)

mistic
mistic

Reputation: 56

If I understand your problem in a right way, the only thing you have to do is declare these two variables as class variables (static or not). So the final code is:

import java.io.File;
import java.util.Scanner;
public class Main {

    public static Scanner kybd = new Scanner(System.in);

    public static int diet = 5;
    public static int happiness = 10;

    public static void main(String[] args) {
        game();
    }

Regards

EDIT FOR DOG DIE

public static die() {
  diet=5;
  happiness=10;
}

Call this function everytime you that your dog die before call game().

Upvotes: 1

Related Questions