0

I'm trying to get the two pets to have two different emotional state. But when I run it they share the same state as seen below:

Oh cool, asda is a very unique name!
asda is DANGEROUS RIGHT NOW!!!.
Oh cool, polo is a very unique name!
polo is DANGEROUS RIGHT NOW!!!.

asda's irrtability level is 4
asda's thirst level is 8
asda's hunger level is 5

polo's irrtability level is 4
polo's thirst level is 8
polo's hunger level is 5

I've stored the value for the emotional states in an array.

I've tried using a for loop so that each pet would have a different value but I'm not sure if I did it correctly.

So should I just create two different array's for the emotional states, or is there a better way of doing things?

here is the whole code.

import javax.swing.*;
import java.util.Random;
public class alientpetprogram
{

public static void main(String[] args)
{   

    int[] EmotionalState = new int [3];
    Random emotion = new Random();
    for(int i = 0; i <= 2; i++)
    {
    int hungerLVL = emotion.nextInt(10) + 1;
    EmotionalState[0] = hungerLVL;

    int thirstLVL = emotion.nextInt(10) + 1;
    EmotionalState[1] = thirstLVL;

    int irritabilityLVL = emotion.nextInt(10) + 1;
    EmotionalState[2] = irritabilityLVL;
    }

    String [] petName = new String [2];
    petEmotion(EmotionalState, petName);

    System.exit(0);
} //ENDS main

    public static String[] petInteraction(int[] EmotionalState, String [] petName) //Use this further on in petEmotion()
    {
        for(int i = 0; i < 2; i++)
        {
            petName[i] = JOptionPane.showInputDialog("What is your pet called?");
            System.out.println("Oh cool, " + petName[i] + " is a very unique name!");


            if (EmotionalState[0] == 1 || EmotionalState[0] == 2 || EmotionalState[0] == 3)
            {
                System.out.println(petName[i] + " is feeling Calm.");
            }
            else if (EmotionalState[0] == 4 || EmotionalState[0] == 5 || EmotionalState[0] == 6 )
            {
                System.out.println(petName[i] + " is feeling tetchy!");
            }
            else if (EmotionalState[0] == 7 || EmotionalState[0] == 8 || EmotionalState[0] == 9 || EmotionalState[0] == 10 )
            {
                System.out.println(petName[i] + " is DANGEROUS RIGHT NOW!!!.");
            }

        }   
        return petName;
    } //ENDS petInteraction

                public static void petEmotion(int[] EmotionalState, String [] petName) //This method changes the emotional states of the pet.
                {  
                      String[] petsName = petInteraction(EmotionalState, petName);

                      String userinput;
                      userinput=JOptionPane.showInputDialog("choose how many rounds?");
                      int roundsuggestion=Integer.parseInt(userinput);



                        for (int round =1; round <=roundsuggestion; round++) //sets the amount of rounds the game runs for.           
                        {
                        System.out.println("Round " + roundsuggestion);                   
                        System.out.println(petsName[0] + "'s irrtability level is " + EmotionalState[2]);
                        System.out.println(petsName[0] + "'s thirst level is " + EmotionalState[1]);
                        System.out.println(petsName[0] + "'s hunger level is " + EmotionalState[0]); 
                        System.out.println(petsName[1] + "'s irrtability level is " + EmotionalState[2]);
                        System.out.println(petsName[1] + "'s thirst level is " + EmotionalState[1]);
                        System.out.println(petsName[1] + "'s hunger level is " + EmotionalState[0]);   


                       for(int y=1; y<=2; y++)
                       {
                            String askToReduceIrritable = JOptionPane.showInputDialog("Would you like to sing for " + petsName[0]  + " in order to lower the pets irritability level?");

                            if (askToReduceIrritable.equalsIgnoreCase("Yes"))
                            {
                                EmotionalState[2] = EmotionalState[2] - 1;
                                System.out.println(petsName[0] + "'s irrtability level is now " + EmotionalState[2]);                           
                            }   

                            String askToReduceThirst = JOptionPane.showInputDialog("Would you like to give " + petsName[0] + " some water in order to reduce the thirst level?");

                            if (askToReduceThirst.equalsIgnoreCase("Yes"))
                            {
                                EmotionalState[1] = EmotionalState[1] - 1;
                                System.out.println(petsName[0] + "'s thirst level is now " + EmotionalState[1]);                           
                            }   

                            String askToReduceHunger = JOptionPane.showInputDialog("Would you like to give " + petsName[0] + " some food in order to reduce the hunger level?");

                            if (askToReduceHunger.equalsIgnoreCase("Yes"))
                            {
                                EmotionalState[0] = EmotionalState[0] - 1;
                                System.out.println(petsName[0] + "'s hunger level is now " + EmotionalState[0]);                           
                            }           
                            System.out.println("");
                            System.out.println("You will now take care of the second pet");

                            String askToReduceIrritableTwo = JOptionPane.showInputDialog("Would you like to sing for " + petsName[1]  + " in order to lower the pets irritability level?");

                            if (askToReduceIrritableTwo.equalsIgnoreCase("Yes"))
                            {
                                EmotionalState[2] = EmotionalState[2] - 1;
                                System.out.println(petsName[1] + "'s irrtability level is now " + EmotionalState[2]);                           
                            }   

                            String askToReduceThirstTwo = JOptionPane.showInputDialog("Would you like to give " + petsName[1] + " some water in order to reduce the thirst level?");

                            if (askToReduceThirstTwo.equalsIgnoreCase("Yes"))
                            {
                                EmotionalState[1] = EmotionalState[1] - 1;
                                System.out.println(petsName[1] + "'s thirst level is now " + EmotionalState[1]);                           
                            }   

                            String askToReduceHungerTwo = JOptionPane.showInputDialog("Would you like to give " + petsName[1] + " some food in order to reduce the hunger level?");

                            if (askToReduceHungerTwo.equalsIgnoreCase("Yes"))
                            {
                                EmotionalState[0] = EmotionalState[0] - 1;
                                System.out.println(petsName[1] + "'s hunger level is now " + EmotionalState[0]);                           
                            }           
                            String exitGame = JOptionPane.showInputDialog("Would you like to exit the game? Type yes/no");

                            if (exitGame.equalsIgnoreCase("Yes"))
                            {
                                System.exit(0);                       
                            }
                            else
                            System.out.println("");
                            JOptionPane.showMessageDialog(null, "A new round has begun!");
                        } // END second loop
           } // END first loop
                }//ENDS petEmotion                 

} //ENDS Class alientpetprogram
1
  • I highly recommend you learn object oriented programming FAST for this type of projects where it involves states. You can simply have a base constructor for a pet object, with initial "states" like hungerLVL, thirstLVL, irritiabilityLVL, etc. Your constructor allows you to change these values as you please, e.g. new Pet(5, 6, 10) respectively for those LVLs. Your methods can retrieve or change those states. Don't do procedural for this, as it makes programming hard, and makes future maintenance incredibly annoying. Commented Dec 9, 2013 at 21:29

1 Answer 1

1

That is because you are using the same value from the EmotionalState array. In your method petInteraction(), the EmotionalState index you are using is 0 always. It is not changing. So all the pets fetched in the for loop would end up having the emotion according to whatever integer is stored in the 0th index of the array. You need to use different indices to get different values from the array. Also, according to your code the indices you use should have a difference of 3 to get them have two different emotions at any time.

EDIT

In your method, you have a for loop which loops two times.Each time for a new pet. Now after you get the name of the pet, you have an if-else construct. No say your EmotionalState array holds value 10 in the 0th index. For the first pet, it will go into the third if. It will print that it is dangerous. In the second run of the for loop, the pet name changes, but the value in the 0th index is still 10. So it will again print that it is dangerous now. The first two if blocks are never entered because the value in the 0th index is always 10 and never changes in the program. That is what is causing it to print the same emotional state. What you can do is you can generate a random number in each run of the for loop and update the 0th index of the EmotionalState array and these two numbers should have atleast a difference of 3 so that for each run it enters a different if block.

Sign up to request clarification or add additional context in comments.

6 Comments

i've used emotionalstate[0] because that holds the hungerlvl. And depending on the level, I want to print a corresponding message. I can't think of another way of doing it. I didn't understand what you meant with having a difference of 3.
@AlexiaBatyn I have explained it in the edited section of my answer.
I've added EmotionalState[i] = (int)(Math.random()*0+9); at the end of the for loop And it appears to generate two random statse for the pet names now
If you are storing the hunger level in 0th index and you need that in your above specified method, it is the value held in the 0th index that would have to be changing. The index will remain 0. So waht you need is, EmotionalState[0] = (int)(Math.random()*0+9)
The first part is working fine now, however in the second part it shows po's irrtability level is 8 po's thirst level is 1 po's hunger level is 9 ad's irrtability level is 8 ad's thirst level is 1 ad's hunger level is 9 Do I place EmotionalState[0/1/2] = (int)(Math.random()*0+9); in the second method to solve this?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.