-2

I'm creating a program that is like a pet bunny simulator. In the program, you are faced with four days (a method for each of these days) and in these days you are asked to make decisions. For example, my program will ask if you want to feed, pet, or play with the bunny. If the user types "feed", a method will be called that adds to a counter keeping track of how many times the user feeds the bunny. What I think is happening is somewhere in my code, the counters keep resetting back to 0. I'm not sure where, though, or how to fix it.

    import java.io.*;
import java.util.*;
public class BunnyProgram 
{
    static int playCounter;
    static int feedCounter;
    static int cleanLitterCounter;
    static int treatCounter;
    static int petCounter;
    static int loveCounter;
    
    public static void main(String [] args)
    {
        Scanner in = new Scanner(System.in);
        welcomeStatement();
        String bunnyName = nameYourBun();
        DayOne(bunnyName, feedCounter, cleanLitterCounter, treatCounter, petCounter, playCounter);
        DayOneChoice(bunnyName, loveCounter);
        results(bunnyName, playCounter, feedCounter, cleanLitterCounter, treatCounter, petCounter, loveCounter);
        DayTwo(bunnyName, feedCounter, cleanLitterCounter, treatCounter, petCounter, playCounter);
        DayTwoChoice(bunnyName, loveCounter);
        DayThree(bunnyName, feedCounter, cleanLitterCounter, treatCounter, petCounter, playCounter);
        DayThreeChoice(bunnyName, loveCounter);
        DayFour(bunnyName, feedCounter, cleanLitterCounter, treatCounter, petCounter, playCounter);
        DayFourChoice(bunnyName, loveCounter);
        results(bunnyName, playCounter, feedCounter, cleanLitterCounter, treatCounter, petCounter, loveCounter);
    }

    public static void welcomeStatement()
    {
        System.out.println("Hiya!! Welcome to the bunny program. In this program, you'll receive a pet bunny!");
        System.out.println("It's your job to make correct choices over the span of four days and prevent your bunny from running away!");
        System.out.println("It is highly recommended to complete every possible action for every day.");
    }
    
    public static String nameYourBun()
    {
        Scanner in;
        in = new Scanner(System.in);
        String bunnyName;
        System.out.println("\nPlease enter a name for your bunny!");
        bunnyName = in.nextLine(); 
        System.out.println("Aww, so cute! Welcome to the (digital) world, " + bunnyName + "!!");
        System.out.println("Let's get started! :)");
        return bunnyName;
    }
    
    public static void DayOne(String bunnyName, int feedCounter, int cleanLitterCounter, int treatCounter, int petCounter, int playCounter)
    {
        Scanner in;
        in = new Scanner(System.in);
        String answer = null;
        System.out.println("\nDay One");
        System.out.println("_________\n");
        
        boolean runIt = true;
        while (runIt)
        {
        chooseYourAction(bunnyName, feedCounter, cleanLitterCounter, treatCounter, petCounter, playCounter);
        System.out.println("Would you like to complete another action? Yes or no");
        answer = in.nextLine();
            if(answer.charAt(0) == 'N' || answer.charAt(0) == 'n')
            {
                runIt = false;
            }
        }
    }
    
    public static void DayOneChoice(String bunnyName, int loveCounter)
    {
        Scanner in;
        in = new Scanner(System.in);
        String userAnswer;
        int Situation;
        Situation = (int) (Math.random() * 3) + 1;
        if (Situation == 1)
        {
            System.out.println("You watch " + bunnyName + " look longily outside.");
            System.out.println("Do you let " + bunnyName + " outside? Yes or no");
            userAnswer = in.nextLine();
            if(userAnswer.charAt(0) == 'Y' || userAnswer.charAt(0) == 'y')
            {
                loveCounter = +1;
            }
            if(userAnswer.charAt(0) == 'N' || userAnswer.charAt(0) == 'n')
            {
                loveCounter = -1;
            }
        }
        if (Situation == 2)
        {
            System.out.println(bunnyName + " looks outside with no particular interest or disinterest.");
            System.out.println("Do you let " + bunnyName + " outside? Yes or no");
            userAnswer = in.nextLine();
            if(userAnswer.charAt(0) == 'Y' || userAnswer.charAt(0) == 'y')
            {
                loveCounter += 0;
            }
            if(userAnswer.charAt(0) == 'N' || userAnswer.charAt(0) == 'n')
            {
                loveCounter += 0;
            }
        }
        if (Situation == 3)
        {
            System.out.println("You see " + bunnyName + " look outside with immense fear.");
            System.out.println("Do you let " + bunnyName + " outside? Yes or no");
            userAnswer = in.nextLine();
            if(userAnswer.charAt(0) == 'Y' || userAnswer.charAt(0) == 'y')
            {
                loveCounter += -1;
            }
            if(userAnswer.charAt(0) == 'N' || userAnswer.charAt(0) == 'n')
            {
                loveCounter += 1;
            }
        }

        System.out.println("Okay! Let's go to the next day!");
        
    }

    public static void DayTwo(String bunnyName, int feedCounter, int cleanLitterCounter, int treatCounter, int petCounter, int playCounter)
    {
        Scanner in;
        in = new Scanner(System.in);
        String answer = null;
        System.out.println("\nDay Two");
        System.out.println("_________\n");
        
        boolean runIt = true;
        while (runIt)
        {
        chooseYourAction(bunnyName, feedCounter, cleanLitterCounter, treatCounter, petCounter, playCounter);
        System.out.println("Would you like to complete another action? Yes or no");
        answer = in.nextLine();
            if(answer.charAt(0) == 'N' || answer.charAt(0) == 'n')
            {
                runIt = false;
            }
        }
    }
    
    public static void DayTwoChoice(String bunnyName, int loveCounter)
    {
        Scanner in;
        in = new Scanner(System.in);
        String userAnswer;
            System.out.println(bunnyName + " has continuously been staring over your shoulder at an ad for a new toy.");
            System.out.println("Do you buy " + bunnyName + " the toy? Choose option 1, 2, or 3.\n");
            System.out.println("1. Buy " + bunnyName + " the toy");
            System.out.println("2. Buy " + bunnyName + " a cheaper toy");
            System.out.println("3. Don't buy " + bunnyName + " any toys");
            int num;
            num = in.nextInt();
            switch(num)
            {
            case 1:
                loveCounter += 1;
            
            case 2:
                loveCounter += 0;
                
            case 3:
                loveCounter += -1;
        }

        System.out.println("Okay! Let's go to the next day!");
        
    }
    
    public static void DayThree(String bunnyName, int feedCounter, int cleanLitterCounter, int treatCounter, int petCounter, int playCounter)
    {
        Scanner in;
        in = new Scanner(System.in);
        String answer = null;
        System.out.println("\nDay Three");
        System.out.println("_________\n");
        
        boolean runIt = true;
        while (runIt)
        {
        chooseYourAction(bunnyName, feedCounter, cleanLitterCounter, treatCounter, petCounter, playCounter);
        System.out.println("Would you like to complete another action? Yes or no");
        answer = in.nextLine();
            if(answer.charAt(0) == 'N' || answer.charAt(0) == 'n')
            {
                runIt = false;
            }
        }
    }
    
    public static void DayThreeChoice(String bunnyName, int loveCounter)
    {
        Scanner in;
        in = new Scanner(System.in);
        String userAnswer;
        int Situation;
        Situation = (int) (Math.random() * 3) + 1;
        if (Situation == 1)
        {
            System.out.println(bunnyName + " has been jumping onto your bed a lot recently.");
            System.out.println("Do you set " + bunnyName + " down on your bed to sleep next to you at night? Yes or no");
            userAnswer = in.nextLine();
            if(userAnswer.charAt(0) == 'Y' || userAnswer.charAt(0) == 'y')
            {
                loveCounter = +1;
            }
            if(userAnswer.charAt(0) == 'N' || userAnswer.charAt(0) == 'n')
            {
                loveCounter = -1;
            }
        }
        if (Situation == 2)
        {
            System.out.println("You catch " + bunnyName + " sleeping on your bed sometimes, and in their own bed other times.");
            System.out.println("Do you set " + bunnyName + " down on your bed to sleep next to you at night? Yes or no");
            userAnswer = in.nextLine();
            if(userAnswer.charAt(0) == 'Y' || userAnswer.charAt(0) == 'y')
            {
                loveCounter += 0;
            }
            if(userAnswer.charAt(0) == 'N' || userAnswer.charAt(0) == 'n')
            {
                loveCounter += 0;
            }
        }
        if (Situation == 3)
        {
            System.out.println(bunnyName + " absolutely loves their bed and seems the most comfortable there.");
            System.out.println("Do you set " + bunnyName + " down on your bed to sleep next to you at night? Yes or no");
            userAnswer = in.nextLine();
            if(userAnswer.charAt(0) == 'Y' || userAnswer.charAt(0) == 'y')
            {
                loveCounter += -1;
            }
            if(userAnswer.charAt(0) == 'N' || userAnswer.charAt(0) == 'n')
            {
                loveCounter += 1;
            }
        }

        System.out.println("Okay! Let's go to the final day!");
        
    }
    
    public static void DayFour(String bunnyName, int feedCounter, int cleanLitterCounter, int treatCounter, int petCounter, int playCounter)
    {
        Scanner in;
        in = new Scanner(System.in);
        String answer = null;
        System.out.println("\nDay Four");
        System.out.println("_________\n");
        
        boolean runIt = true;
        while (runIt)
        {
        chooseYourAction(bunnyName, feedCounter, cleanLitterCounter, treatCounter, petCounter, playCounter);
        System.out.println("Would you like to complete another action? Yes or no");
        answer = in.nextLine();
            if(answer.charAt(0) == 'N' || answer.charAt(0) == 'n')
            {
                runIt = false;
            }
        }
    }
    
    public static void DayFourChoice(String bunnyName, int loveCounter)
    {
        Scanner in;
        in = new Scanner(System.in);
        String userAnswer;
            System.out.println("You notice " + bunnyName + " has been limping.");
            System.out.println("How do you treat " + bunnyName + "? Choose option 1, 2, or 3.\n");
            System.out.println("1. Take " + bunnyName + " to the vet");
            System.out.println("2. Try and take care of " + bunnyName + "'s paw by yourself.");
            System.out.println("3. Ignore " + bunnyName + "'s paw");
            int num;
            num = in.nextInt();
            switch(num)
            {
            case 1:
                loveCounter += 1;
            
            case 2:
                loveCounter += 0;
                
            case 3:
                loveCounter += 1;
        }

        System.out.println("Okay! Let's see how you did!");
    }
    
    public static void chooseYourAction(String bunnyName, int feedCounter, int cleanLitterCounter, int treatCounter, int petCounter, int playCounter)
    {
        Scanner in;
        in = new Scanner(System.in);
        String choice;
        
        
        System.out.println("Would you like to feed " + bunnyName + ", pet " + bunnyName + ", play with ");
        System.out.println(bunnyName + ", clean "  + bunnyName + "'s litter, or give "  + bunnyName + " a treat?");
        choice = in.nextLine();
        YourChoice(choice, bunnyName, feedCounter, cleanLitterCounter, treatCounter, petCounter, playCounter);
        
    }
    
    public static void YourChoice (String choice, String bunnyName, int feedCounter, int cleanLitterCounter, int treatCounter, int petCounter, int playCounter)
    {
        switch (choice.charAt(0))
        {
        
        case 'F':
        case 'f':
            {
            feedCounter = feedBunny(bunnyName, feedCounter);
            break;
            }
        }
        
        switch (choice.charAt(0))
        {
        case 'C':
        case 'c':
        case 'L':
        case 'l':
            {
            cleanLitterCounter = cleanLitter(bunnyName, cleanLitterCounter);
            break;
            }
        }
        
        switch (choice.charAt(0))
        {
        case 'G':
        case 'g':
        case 'T':
        case 't':
            {
            treatCounter = giveTreat(bunnyName, treatCounter);
            break;
            }
        }
        
        switch (choice.charAt(0))
        {
        case 'P':
        case 'p':
            {
                switch (choice.charAt(1))
                {
                case 'E':
                case 'e':
                {
                    petCounter = petBunny(bunnyName, petCounter);
                    break;
                }
                
                case 'L':
                case 'l':
                {
                    playCounter = playWithBunny(bunnyName, playCounter);
                    break;
                }
                    
                }
            }
        }
    }
    
    public static int feedBunny(String bunnyName, int feedCounter)
    {
        Scanner in;
        in = new Scanner(System.in);
        System.out.println("How many times will you feed " + bunnyName + " today?");
        feedCounter += in.nextInt();
        if (feedCounter == 1)
        {
            feedCounter += 0;
        }
        if(feedCounter == 2)
        {
            feedCounter += 1;
        }
        if(feedCounter == 0)
        {
            feedCounter += -1;
        }
        if(feedCounter > 2)
        {
            feedCounter += -2;
        }
        return feedCounter;
    }
    
    public static int cleanLitter(String bunnyName, int cleanLitterCounter)
    {
        Scanner in;
        in = new Scanner(System.in);
        System.out.println("How many times will you clean " + bunnyName + "'s litter today?");
        cleanLitterCounter += in.nextInt();
        if (cleanLitterCounter == 1)
        {
            cleanLitterCounter += 1;
        }
        if(cleanLitterCounter == 2 || cleanLitterCounter == 0)
        {
            cleanLitterCounter += -1;
        }
        return cleanLitterCounter;
    }
    
    public static int giveTreat(String bunnyName, int treatCounter)
    {
        Scanner in;
        in = new Scanner(System.in);
        System.out.println("How many times will you give " + bunnyName + " a treat today?");
        treatCounter += in.nextInt();
        if (treatCounter == 1)
        {
            treatCounter += 1;
        }
        if(treatCounter == 2)
        {
            treatCounter += -1;
        }
        if(treatCounter == 0)
        {
            treatCounter += 0;
        }
        if(treatCounter > 2)
        {
            treatCounter += -2;
        }
        return treatCounter;
    }
    
    public static int petBunny(String bunnyName, int petCounter)
    {
        Scanner in;
        in = new Scanner(System.in);
        System.out.println("How many times will you pet " + bunnyName + " today?");
        petCounter += in.nextInt();
        if (petCounter == 1)
        {
            petCounter += 0;
        }
        if(petCounter == 2)
        {
            petCounter += 1;
        }
        if(petCounter == 0 || petCounter > 2)
        {
            petCounter += -1;
        }
        return petCounter;
    }
    
    public static int playWithBunny(String bunnyName, int playCounter)
    {
        Scanner in;
        in = new Scanner(System.in);
        System.out.println("How many times will you play with " + bunnyName + " today?");
        playCounter += in.nextInt();
        if (playCounter == 1)
        {
            playCounter += 1;
        }
        if(playCounter == 2)
        {
            playCounter += 2;
        }
        if(playCounter == 0)
        {
            playCounter += -1;
        }
        return playCounter;
    }
    
    public static int loveTotal (int playCounter, int feedCounter, int cleanLitterCounter, int treatCounter, int petCounter, int loveCounter)
    {
        loveCounter = playCounter + feedCounter + cleanLitterCounter + treatCounter + petCounter;
        return loveCounter;
    }

    public static void results (String bunnyName, int playCounter, int feedCounter, int cleanLitterCounter, int treatCounter, int petCounter, int loveCounter)
    {
        int lC = loveTotal(playCounter, feedCounter, cleanLitterCounter, treatCounter, petCounter, loveCounter);
        
        System.out.println(lC);
        if(lC >= 18)
        {
            System.out.println(bunnyName + " comes up to you to give you a million kisses!");
            System.out.println("You're an amazing bunny parent! <3");
        }
        
        if(lC <= 17 && lC >= 0)
        {
            System.out.println(bunnyName + " looks at you with a blank expression and hops the other way.");
            System.out.println("You're an alright bunny parent.. but more effort would be appreciated.");
        }
        if(lC < 0)
        {
            System.out.println("You wake up one morning to find " + bunnyName + "'s area vacant.");
            System.out.println("Maybe if you would have been a better bunny parent things would have turned out differently..");
        }
    }
}

At the end, I have my results printed and it won't stop printing 0. I don't know what to do and I feel like everything I've tried hasn't been working. I initialized my variable in the public class but that didn't help anything. I also have each variable set to "+=" so I have no idea why it isn't adding the values each time the method is called.

Sabrina
  • 1
  • 1
  • 1
    We need to see more code. Please consider editing the question so there is a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Old Dog Programmer Dec 18 '22 at 19:54
  • Hello, what is the point of the ifs? Looks to me like the feedCounter isn't always 0 like you say. – Stefan Dec 18 '22 at 19:54
  • 1
    However, there is a good chance the problem is with the `DayOne` method and the way it is called. Each of the `int` parameters are variables local to the `DayOne` method. They begin with values that are copies of the corresponding arguments in the method call. So, changing the values within `DayOne` does not result in changing the corresponding values in the method(s) that call `DayOne`. – Old Dog Programmer Dec 18 '22 at 19:57
  • 1
    We're not going to be able to find your problem, if you only show us a small sample of your program. Most likely, your problem is in a part that you haven't shown here. – Dawood ibn Kareem Dec 18 '22 at 19:58
  • Off Topic: Please consider having only one `Scanner` Object for `System.in`. You may make `in` a `static` variable for your class. Or, you can pass it via argument lists to methods that need to use it. – Old Dog Programmer Dec 18 '22 at 20:08
  • @OldDogProgrammer I just added my entire code since I'm not 100% sure what exactly I need to show – Sabrina Dec 18 '22 at 20:27
  • @DawoodibnKareem I added my entire code. I'm not sure what exactly I need to show – Sabrina Dec 18 '22 at 20:28

1 Answers1

2

Consider these lines:

public class BunnyProgram 
{
   static int playCounter;
   static int feedCounter;
   static int cleanLitterCounter;
   static int treatCounter;
   static int petCounter;
   static int loveCounter;

   public static void main(String [] args)
   {
       Scanner in = new Scanner(System.in);
       welcomeStatement();
       String bunnyName = nameYourBun();
       DayOne(bunnyName, feedCounter, cleanLitterCounter, treatCounter
             , petCounter, playCounter);

And the beginning of this method:

  public static void DayOne
    (String bunnyName, int feedCounter, int cleanLitterCounter
    , int treatCounter, int petCounter, int playCounter)
{

Your mistake is passing the int counters as arguments. Within method DayOne, the variable feedCounter is a different variable than the static int feedCounter declared above static void main. Both variables have the same name, but the local variable hides the static variable. The same applies to the other int variables passed. So, when a value of one of them is changed within the method, it is not changed within main.

To fix the problem, don't pass them:

 public static void DayOne (String bunnyName) { 

and, in main

 DayOne (bunnyName); 

That way, the only feedCounter, playCounter, etc. the program sees are the static ones.

This happens because Java is pass by value: When calling a method, the values of the arguments are copied to the local variables in the parameter list of the called method.

This appears to not happen with bunnyName. That is because bunnyName is a String, which is a reference type. The counters are primitives.

With reference types, what gets passed to a called method is a copy of the reference. The local variable in the called method becomes an alias for the corresponding variable doing the calling. Within the called method, as long as the local reference variable isn't changed to point to something else, what happens to it in the called method also happens to it in the calling method.

Off-topic: While you are at it, add static Scanner in; to the list of static variables. At the beginning of main, have in = new Scanner (System.in);. That way, you don't have to open a new Scanner (System.in) in other methods.

To learn more, search for "Java variable shadowing" and "Java call by value"

Old Dog Programmer
  • 901
  • 1
  • 7
  • 9
  • These two questions might help: https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value https://stackoverflow.com/questions/16941179/variable-shadowing-in-java – Old Dog Programmer Feb 01 '23 at 00:47