gpt4 book ai didi

java - hive 模拟 : A counter of a type of bee returns unusually high numbers

转载 作者:行者123 更新时间:2023-12-01 19:34:21 24 4
gpt4 key购买 nike

我正在构建 hive 模拟,并使用名为 workerBee 的二维数组。它有以下 6 个字段:beeId、Age、Type(egg = 1、larva = 2、pupa = 3、worker = 4、drone = 5)、PollenCollection、Eaten、Alive

模型简介:蜂王每天产卵(10 到 50 个卵),并将它们添加到 hive 中。每天,有关蜜蜂的数据都会更新(它们的年龄和类型)。

对于过去的每一天,我都会打印 hive 状态,其中打印有关蜜蜂数量、出生、死亡等的信息。在模拟过程中的某些日子(开始时,例如第 6 至 10 天),报告的幼虫数量为 1 天约 800 至 900 只。以下是处理打印和计数的代码:

    public static int layDailyEggs() {
Random randomEggs = new Random();
final int MAX_EGGS = 50;
final int MIN_EGGS = 10;

int x = randomEggs.nextInt((MAX_EGGS - MIN_EGGS) + 1) + MIN_EGGS;
eggsLaid = x;//eggsLaid as a global variable to be used in printBeehiveStatus
return x;//To pass as argument to addEggToHive
}
public static void addEggToHive(int eggsLaid) {
//Update the workerBee array with available slots
//Traverse the 2D array and while beeId != 0, add eggs and update
for (int i = 0; i < workerBee.length; i++) {

if (workerBee[i][0] == 0 && eggsLaid > 0) {
//Available space
workerBee[i][0] = i;//Update beeID
workerBee[i][1] = 1;//Update age
workerBee[i][2] = 1;//Update Type
eggsLaid--;
}
}
}
public static void countTypesOfBees() {
//Initialize for each day
totalEggsLaid = 0;
numberOfBirths = 0;
numberOfLarva = 0;
numberOfPupa = 0;
numberOfWorkerBees = 0;
numberOfDrones = 0;

//To call upon updating type of each bee
for (int i = 0; i < workerBee.length; i++) {

if(workerBee[i][2] == 1) {
totalEggsLaid++;
}else if(workerBee[i][2] == 2) {
numberOfLarva++;
numberOfBirths++;
}else if(workerBee[i][2] == 3) {
numberOfPupa++;
numberOfBirths++;
}else if(workerBee[i][2] == 4) {
numberOfWorkerBees++;
numberOfBirths++;
}else if(workerBee[i][2] == 5) {
numberOfDrones++;
numberOfBirths++;
}
}
}
//Method called once daily
public static void metamorphose() {
numberOfDeaths = 0;
Random random = new Random();

for (int i = 0; i < workerBee.length; i++) {

//Updating the type of bee based on age of Bee
if(workerBee[i][1] == 4) {
workerBee[i][2] = 2;
}else if (workerBee[i][1] == 10) {
workerBee[i][2] = 3;
}else if(workerBee[i][1] == 20){
//Probability for a drone to emerge is 10%(As per area under curve, should be less than or equal to 10%)
double probability = random.nextDouble();
if (probability <= 0.1) {
workerBee[i][2] = 5;//Drone Bee
}else{
workerBee[i][2] = 4;//Worker Bee
}
}
if (workerBee[i][1] == 35 || workerBee[i][1] == 56) {
//Call a funeral
funeral(i);
numberOfDeaths++;
}

}
countTypesOfBees();
}
//To be called at the end of the day
public static void printBeehiveStatus() {

System.out.print("Queen laid " + eggsLaid + " eggs!" +
"\nBeehive status\nEgg Count: "+ totalEggsLaid + "\nLarva Count: " + numberOfLarva + "\nPupa Count: " + numberOfPupa +
"\nWorker Count: "+ numberOfWorkerBees + "\nDrone Count: " + numberOfDrones +
"\nDeath Count: " + numberOfDeaths + "\nBirth Count: "+ numberOfBirths +
"\nHoney Stock: " + honeyStock +"\n");
printFlowerGarden();
}

workerBee数组的字段索引按照上面指定的顺序排列。每天执行的顺序如下(注意不是完整的)

    addEggToHive(layDailyEggs());
incrementAge();
metamorphose();
printBeehiveStatus();

屏幕截图[1]:/image/qTeuo.png
截图[2]:/image/eMsHq.png

注意
卵在4天大时孵化成幼虫如果您认为还有其他可能导致问题的原因,请告诉我,我将上传该部分代码。

最佳答案

找到了解决方案。实际上,incrementAge() 方法应该每天将 hive 中所有蜜蜂的年龄增加 1。然而,我只是简单地增加数组中的所有年龄,而不检查该特定行是否是实际的蜜蜂,因为我已将未使用的行初始化为0

关于java - hive 模拟 : A counter of a type of bee returns unusually high numbers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59234595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com