如何调用 for 循环内的实例变量?
How do I call an instance variable that is inside a for loop?
我想调用一个使用方程式的实例变量。我需要 for 循环中的 "i" 来计算总价。我的变量 bonePrice 在 for 循环内部或外部都不起作用。
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(".00 spent feeding 1 cow(s) 5 total feedings"
+ "\n.00 spent feeding 1 cat(s) 5 total feedings"
+ bonePrice +"\nspent feeding 1 dog(s) 5 total feedings"
+ "\n\tTotal Cost: " + /*total cost**/);
它需要在for
循环的范围之外声明
例如
double bonePrice = 0.00;
for (int i = 0; i < stalls.size(); i++){
..
bonePrice += boneCost* stalls.get(i).numberOfFeedings;
}
System.out.println(".00 spent feeding 1 cow(s) 5 total feedings"
+ "\n.00 spent feeding 1 cat(s) 5 total feedings"
+ bonePrice +"\nspent feeding 1 dog(s) 5 total feedings"
+ "\n\tTotal Cost: " + /*total cost**/);
当你说 double bonePrice
时,你实际上已经定义了一个 new 局部变量,它通过相同的方式掩盖了任何 instance 变量名字.
当你在循环中这样做时,这个 local bonePrice
在循环之外是不可见的。如果将此声明移到循环外,它在整个方法中都可见,但仍会阻止访问实例变量。
因此,要将值分配给 instance 成员,只需使用它的名称
bonePrice = boneCost* stalls.get(i).numberOfFeedings;
如果 bonePrice
应该保持 运行 总计 那么您可能需要向其添加成本而不是为其分配新值在循环的每次迭代中。
bonePrice += boneCost* stalls.get(i).numberOfFeedings;
最后,有时您可能决定让局部变量的名称与 instance 成员的名称相同(在 constuctor 参数例如)。在这种情况下,您可以通过显式使用 this
关键字来消除歧义。
this.bonePrice = bonePrice; // instance var = local var here
您不能在声明的地方之外使用实例变量。如果你想使用它,只需在 for 循环之外声明它。
我想调用一个使用方程式的实例变量。我需要 for 循环中的 "i" 来计算总价。我的变量 bonePrice 在 for 循环内部或外部都不起作用。
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(".00 spent feeding 1 cow(s) 5 total feedings"
+ "\n.00 spent feeding 1 cat(s) 5 total feedings"
+ bonePrice +"\nspent feeding 1 dog(s) 5 total feedings"
+ "\n\tTotal Cost: " + /*total cost**/);
它需要在for
循环的范围之外声明
例如
double bonePrice = 0.00;
for (int i = 0; i < stalls.size(); i++){
..
bonePrice += boneCost* stalls.get(i).numberOfFeedings;
}
System.out.println(".00 spent feeding 1 cow(s) 5 total feedings"
+ "\n.00 spent feeding 1 cat(s) 5 total feedings"
+ bonePrice +"\nspent feeding 1 dog(s) 5 total feedings"
+ "\n\tTotal Cost: " + /*total cost**/);
当你说 double bonePrice
时,你实际上已经定义了一个 new 局部变量,它通过相同的方式掩盖了任何 instance 变量名字.
当你在循环中这样做时,这个 local bonePrice
在循环之外是不可见的。如果将此声明移到循环外,它在整个方法中都可见,但仍会阻止访问实例变量。
因此,要将值分配给 instance 成员,只需使用它的名称
bonePrice = boneCost* stalls.get(i).numberOfFeedings;
如果 bonePrice
应该保持 运行 总计 那么您可能需要向其添加成本而不是为其分配新值在循环的每次迭代中。
bonePrice += boneCost* stalls.get(i).numberOfFeedings;
最后,有时您可能决定让局部变量的名称与 instance 成员的名称相同(在 constuctor 参数例如)。在这种情况下,您可以通过显式使用 this
关键字来消除歧义。
this.bonePrice = bonePrice; // instance var = local var here
您不能在声明的地方之外使用实例变量。如果你想使用它,只需在 for 循环之外声明它。