Java 蜂巢模拟:一种蜜蜂的计数器返回异常高的数值
我正在构建一个蜂巢模拟,我使用了一个名为workerBee的2D数组。 它有以下6个字段:蜂种、年龄、类型(卵=1、幼虫=2、蛹=3、工蜂=4、雄蜂=5)、花粉采集、食用、存活 模型简介:蜂王每天产卵(10到50个卵),并将它们添加到蜂箱中。每天更新有关蜜蜂的数据(它们的年龄和类型) 对于过去的每一天,我都会打印蜂箱状态,其中会打印有关蜜蜂数量、出生、死亡等信息。。 在模拟过程中的一些天(开始时,比如第6到第10天),报告的幼虫数量约为800到900只,持续1天。 以下是处理打印和计数的代码:Java 蜂巢模拟:一种蜜蜂的计数器返回异常高的数值,java,arrays,multidimensional-array,count,Java,Arrays,Multidimensional Array,Count,我正在构建一个蜂巢模拟,我使用了一个名为workerBee的2D数组。 它有以下6个字段:蜂种、年龄、类型(卵=1、幼虫=2、蛹=3、工蜂=4、雄蜂=5)、花粉采集、食用、存活 模型简介:蜂王每天产卵(10到50个卵),并将它们添加到蜂箱中。每天更新有关蜜蜂的数据(它们的年龄和类型) 对于过去的每一天,我都会打印蜂箱状态,其中会打印有关蜜蜂数量、出生、死亡等信息。。 在模拟过程中的一些天(开始时,比如第6到第10天),报告的幼虫数量约为800到900只,持续1天。 以下是处理打印和计数的代码:
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();
}
屏幕截图[1]:截图[2]: 注
卵在4天大时孵化成幼虫 如果您认为还有其他问题可能导致此问题,请告诉我我将上载该部分代码。找到了解决方案。
实际上,方法incrementAge()应该使蜂箱中所有蜜蜂的年龄每天增加1岁。但是,我只是简单地增加数组中的所有年龄,而没有检查特定行是否是实际的蜜蜂,因为我已将未使用的行初始化为0Java是一种面向对象的语言。我建议尝试使用类和对象来抽象任务,而不是使用
int
的二维数组。在当前状态下,您的代码很难理解。我理解,但这是一门课程。目前只需要模块化编程。我希望我能这样做,我知道这是可能的。但是我必须满足课程的要求。好吧,在这种情况下,我怀疑你的静态变量有问题。例如,您有一个eggsLaid
静态变量,在某些方法中,您有一个同名的局部变量。如果您不更新您认为正在更新的变量,这可能会导致问题。如果必须使用静态变量,至少不要给它们与局部变量相同的名称。我尝试过更改它,但仍然没有更改。请在问题中使用。
addEggToHive(layDailyEggs());
incrementAge();
metamorphose();
printBeehiveStatus();