简体   繁体   中英

How do I call an instance variable that is inside a for loop?

I want to call an instance variable that uses an equation. I need "i" from the for loop to calculate the total price. My variable bonePrice does not work inside the for loop or outside.

 for (int i = 0; i < stalls.size(); i++){
        if (stalls.get(i).speak().equals("MOO")){
            while (stalls.get(i).isStillHungry() == false)  {
                stalls.get(i).feed(FoodType.GRASS);
                stalls.get(i).isStillHungry();
                }
            System.out.println("Stall " + (i+1) + " of " + stalls.size() + " contains a cow who had " + stalls.get(i).numberOfFeedings + " feedings, which cost " + df.format(grassCost * stalls.get(i).numberOfFeedings));

        }

    if (stalls.get(i).speak().equals("WOOF")){
        while (stalls.get(i).isStillHungry() == false)  {
            stalls.get(i).feed(FoodType.BONE);
            stalls.get(i).isStillHungry();
            }
        System.out.println("Stall " + (i+1) + " of " + stalls.size() + " contains a dog who had " + stalls.get(i).numberOfFeedings + " feedings, which cost " + df.format(boneCost* stalls.get(i).numberOfFeedings));

        }

    if (stalls.get(i).speak().equals("MEOW")){
        while (stalls.get(i).isStillHungry() == false)  {
            stalls.get(i).feed(FoodType.SALMON);    
            stalls.get(i).isStillHungry();
            }
        System.out.println("Stall " + (i+1) + " of " + stalls.size() + " contains a cat who had " + stalls.get(i).numberOfFeedings + " feedings, which cost " + df.format(salmonCost* stalls.get(i).numberOfFeedings));

         double bonePrice = boneCost* stalls.get(i).numberOfFeedings;
         double grassPrice = grassCost* stalls.get(i).numberOfFeedings;
         double salmonPrice = salmonCost* stalls.get(i).numberOfFeedings;
                }


    }       

    System.out.println("Old MacDonald's total expenses: ");
    System.out.println("$5.00 spent feeding 1 cow(s) 5 total feedings"
                        + "\n$25.00 spent feeding 1 cat(s) 5 total feedings"
                        + bonePrice +"\nspent feeding 1 dog(s) 5 total feedings" 
                        + "\n\tTotal Cost: " + /*total cost**/);

It needs to be declared outside of the scope of the for loop

eg

double bonePrice = 0.00;

for (int i = 0; i < stalls.size(); i++){

    ..

    bonePrice += boneCost* stalls.get(i).numberOfFeedings;
}

System.out.println("$5.00 spent feeding 1 cow(s) 5 total feedings"
                    + "\n$25.00 spent feeding 1 cat(s) 5 total feedings"
                    + bonePrice +"\nspent feeding 1 dog(s) 5 total feedings" 
                    + "\n\tTotal Cost: " + /*total cost**/);

When you say double bonePrice you've actually defined a new local variable that overshadows any instance variable by the same name.

When yo do so within the loop, this local bonePrice is not visible outside of it. If you move this declaration to outside the loop, it becomes visible throughout the method but still prevents access to the instance variable.

So, to assign the value to the instance member, just use it's name like

bonePrice = boneCost* stalls.get(i).numberOfFeedings;

If the bonePrice is supposed to keep a running total then you probably need to add the costs to it instead of assigning it a new value at every iteration of the loop.

bonePrice += boneCost* stalls.get(i).numberOfFeedings;

Finally, there are times when you may decide to leave a local variable's name the same as that of an instance member (in constuctor arguments for example). In such cases, you can remove the ambiguity by making an explicit use of the this keyword.

this.bonePrice = bonePrice; // instance var = local var here

You can't use the instance variable outside where it is declared. If you want to use it just declare it outside the for loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM