对于我的作业问题,这是一个带有签名public int applyNutrientCoefficient()
的方法,用于计算Guppies
中哪个Pool
已经死于营养不良,并返回死亡人数。
使用Iterator<Guppy>
来迭代guppiesInPool
中的Pool
。对于每个Guppy
,使用Random
方法nextDouble()
生成介于0.0和1.0之间的不同随机数。如果这是随机生成的
数量大于池的营养系数,杀死Guppy
通过在Guppy
中设置适当的布尔字段。请注意这一点
方法不会从池中移除任何死亡的孔雀鱼,它只是杀死
他们。别做其他事。
我有两个班级一个是Guppy
一个是Pool
在我的guppy类中,我创建了一个布尔值 -
private boolean isAlive{}
public boolean getIsAlive(){
return isAlive
}
在我的Pool类....
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()
有人可以帮忙吗
答案 0 :(得分:0)
您必须通过您提供的公共getter访问isAlive私有字段getIsAlive()
在Pool类
中if (randomNumberGenerator.nextDouble() > nutrientCoefficient)
if (guppyOne.isAlive() )
guppyOne.setAlive(false)
deathCount++
return deathCount
行
if (guppyOne.isAlive() )
应该是
if (guppyOne.getIsAlive() )
与setter相同:你需要为Guppy类提供一个setter并使用它
public void setIsAlive(boolean alive){
this.isAlive = alive}
最终结果应为
if (randomNumberGenerator.nextDouble() > nutrientCoefficient)
if (guppyOne.getIsAlive() )
guppyOne.setIsAlive(false)
deathCount++
return deathCount
答案 1 :(得分:0)
你的语法似乎有点不对劲。它应该是
public class FilterAsync extends AsyncTask {
private FFmpegFrameGrabber VIDEO_GRABBER;
private FFmpegFrameRecorder videoRecorder;
private File file = new File(Environment.getExternalStorageDirectory() + "/Download/Abc.mp4");
private Context mContext;
private FFmpegFrameFilter filter;
private boolean isTrue = false;
private ArrayList<String> videoPaths;
private File myDirectory;
public FilterAsync(Context context) {
mContext = context;
VIDEO_GRABBER = new FFmpegFrameGrabber(file);
myDirectory = new File(Environment.getExternalStorageDirectory() + "/Folder/");
if (!myDirectory.exists()) {
myDirectory.mkdirs();
}
videoPaths = new ArrayList<>();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Object doInBackground(Object[] params) {
Frame tempVideoFrame;
try {
VIDEO_GRABBER.start();
initVideoRecorder(myDirectory + "/video" + System.currentTimeMillis() + ".mp4");
filter.start();
while (VIDEO_GRABBER.grab() != null) {
tempVideoFrame = VIDEO_GRABBER.grabImage();
if (tempVideoFrame != null) {
filter.push(tempVideoFrame);
tempVideoFrame = filter.pull();
videoRecorder.record(tempVideoFrame);
}
}
videoRecorder.stop();
filter.stop();
videoRecorder.release();
VIDEO_GRABBER.stop();
VIDEO_GRABBER.release();
} catch (FrameGrabber.Exception e) {
e.printStackTrace();
} catch (FrameRecorder.Exception e) {
e.printStackTrace();
} catch (FrameFilter.Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
}
private void initVideoRecorder(String path) {
try {
filter = new FFmpegFrameFilter("transpose=clock ,crop=w=640:h=480:x=0:y=0", VIDEO_GRABBER.getImageWidth(), VIDEO_GRABBER.getImageHeight());
videoRecorder = FFmpegFrameRecorder.createDefault(path, VIDEO_GRABBER.getImageWidth(), VIDEO_GRABBER.getImageHeight());
videoRecorder.setAudioChannels(VIDEO_GRABBER.getAudioChannels());
videoRecorder.start();
} catch (FrameRecorder.Exception e) {
e.printStackTrace();
}
}
}
然后,
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;
}
还要确保使用分号和花括号,否则编译器会给你错误。