在for循环中更新我的变量?

时间:2015-02-03 02:02:17

标签: java for-loop

我试图在每次迭代时更新我的​​变量startPreyPopulationSize和startPredatorPopulation大小,但我不知道如何去做,因为底部for循环会影响到最右边的那个?

    //Equation to find out prey population size.
    for(double rate = 0; rate <= numberOfPeriods; rate++) {

        double partOne = startPreyPopulationSize / rate * (1 + RATE_A_BIRTH - RATE_B_PREDATION * (startPredatorPopulationSize / rate));
        double updatedPreyPopulation = startPreyPopulationSize / (rate + 1);
        updatedPreyPopulation = partOne;
        System.out.printf("Period %5.0f: Prey Population %.2f; \n", rate, updatedPreyPopulation);

    }


    //Equation to find out predator population size.
    for(double rate2 = 0; rate2 <= numberOfPeriods; rate2++) {
        double partTwoEquation = startPredatorPopulationSize / rate2 * (1 - RATE_C_DEATHS + RATE_D_INCREASE * (startPreyPopulationSize / rate2));
        double updatedPredatorPopulation = startPredatorPopulationSize / (rate2 + 1);
        updatedPredatorPopulation = partTwoEquation;
        System.out.printf("Period %5.0f: Predator Population %.2f; \n", rate2, updatedPredatorPopulation);
    }

更新后的代码如果有帮助的话,我会将我的公式从这个基础上移开http://i.imgur.com/rUQ774q.jpg我知道我的公式是以某种方式关闭的。

    //Equation to find out prey population size.
    for(double rate = 0; rate < numberOfPeriods; rate++) {

        partOne = startPreyPopulationSize / rate * (1 + RATE_A_BIRTH - RATE_B_PREDATION * (startPredatorPopulationSize / rate));
        currentPreyPopulationSize = startPreyPopulationSize / (rate + 1);
        currentPreyPopulationSize = partOne;
        System.out.printf("Period %5.0f: Prey Population %.2f; \n", rate, currentPreyPopulationSize);

        partTwoEquation = startPredatorPopulationSize / rate * (1 - RATE_C_DEATHS + RATE_D_INCREASE * (startPreyPopulationSize / rate));
        currentPredatorPopulationSize = startPredatorPopulationSize / (rate + 1);
        currentPredatorPopulationSize = partTwoEquation;
        System.out.printf("Period %5.0f: Predator Population %.2f; \n", rate, currentPredatorPopulationSize);


        startPreyPopulationSize = currentPreyPopulationSize;
        startPredatorPopulationSize = currentPredatorPopulationSize;
    }

1 个答案:

答案 0 :(得分:0)

你提供的方程式代码是错误的。

1)在等式中,你没有rate的除法,所以看起来应该是

double currentPreyPopulationSize = startPreyPopulationSize * (1 + RATE_A_BIRTH - RATE_B_PREDATION * (startPredatorPopulationSize));

2)此分区也导致-Infinity,因为第一次迭代rate0