从不同的类布尔麻烦 [英] boolean trouble from different class
问题描述
有关我的任务问题,它与签名公众诠释applyNutrientCoefficient()
,该计算方法,该方法孔雀鱼
在泳池
死于营养不良,并返回死亡人数。
For my assignment question it is a method with the signature public int applyNutrientCoefficient()
, that calculates which Guppies
in the Pool
have died of malnutrition, and returns the number of deaths.
使用的的Iterator<孔雀鱼>
在池<遍历
guppiesInPool
/ code>。对于每个顾比
生成0.0和1.0包容性的使用随机
方法 nextDouble之间不同的随机数()
。如果这个随机生成
数比池的营养系数可,杀死顾比
更大
通过在设置适当的布尔领域孔雀鱼
。注意,这
方法不从池中删除任何死孔雀鱼,它只是杀死
他们。不要做任何事情。
Use an Iterator<Guppy>
to iterate over the guppiesInPool
in the Pool
. For each Guppy
generate a different random number between 0.0 and 1.0 inclusive using the Random
method nextDouble()
. If this randomly generated
number is greater than the Pool's nutrient coeffcient, kill that Guppy
by setting the appropriate boolean field in the Guppy
. Note that this
method does not remove any dead Guppies from the Pool, it just kills
them. Do not do anything else.
我有2个班一个是顾比
一个是泳池
在我的孔雀鱼类我做了一个布尔 -
I have 2 classes one is Guppy
one is Pool
in my guppy class I made a boolean -
private boolean isAlive{}
public boolean getIsAlive(){
return isAlive
}
在我的游泳池类....
in my Pool class....
public int applyNutrientCoefficient()
int deathCount = 0
Iterator<Guppy> it = guppiesInPool.iterator()
while (it.hasNext() )
Guppy guppyOne = it.next()
if (randomNumberGenerator.nextDouble() > nutrientCoefficient)
if (guppyOne.isAlive() )
guppyOne.setAlive(false)
deathCount++
return deathCount
我收到错误消息是无法找到符号 - 方法的isAlive()
有人可以帮助请
推荐答案
您似乎语法有点过。它应该是
Your syntax seems kinda off. It should be
private boolean isAlive; //private field, not a method.
public boolean getIsAlive() { //public getter method.
return isAlive;
}
public void setIsAlive(boolean isAlive) { //public setter method
this.isAlive = isAlive;
}
然后,
public int applyNutrientCoefficient() {
int deathCount = 0;
Iterator it = guppiesInPool.iterator();
while (it.hasNext()) {
Guppy guppyOne = it.next();
if (randomNumberGenerator.nextDouble() > nutrientCoefficient) {
if (guppyOne.getIsAlive()) {
guppyOne.setIsAlive(false);
deathCount++; // should be inside the if-block i suppose?
}
}
}
return deathCount;
}
另外,还要确保你使用分号和大括号或编译器会给你的错误。
Also make sure you use semicolons and curly braces or the compiler will give you errors.
这篇关于从不同的类布尔麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!