麻烦与数学公式 - Java

时间:2016-10-08 07:04:11

标签: java eclipse math

我正在为我的java课程制作一个程序,用于计算开始年份(2011年)的一年人口,并且每年增加1.2%的人口。 2011年的人口是7.000(我使用的是小数,而不是数十亿)。我目前有这段代码。

int startYear = 2011;
int endYear = user_input.nextInt();
double t = 1.2; //Population percent increase anually
double nbr = (endYear - startYear); //Number of years increased
double pStart = 7.000; //Starting population of 2011
double pEnd = pStart * Math.exp(nbr * t); // Ending population of user input
DecimalFormat nf = new DecimalFormat("2");
System.out.println("Population in " + endYear + ": " (nf.format(pEnd)));

代码中没有错误,一切正常,但我对pEnd方程有麻烦。目前,当我在2016年进入2016年时,我得到22824.我已经尝试使用谷歌搜索公式,但我找不到任何东西。你们中的任何人都知道这个公式吗?如果您在年底输入2016,则应该在7.433左右

3 个答案:

答案 0 :(得分:2)

你的增量是1.2倍,代表120%而不是1.2%。我想你想要的是:

double t = 0.012;

此更改在2011年至2016年期间为我提供了精确的值7.4328558258175175。

编辑:这里是作者要求的代码:

public static void main(String args[]){
    int startYear = 2011;
    int endYear = 2016;
    double t = 0.012; //Population percent increase anually
    double nbr = (endYear - startYear); //Number of years increased
    double pStart = 7.000; //Starting population of 2011
    double pEnd = pStart * Math.exp(nbr * t); // Ending population of user input
    System.out.println("Population in " + endYear + ": " + pEnd);
}

答案 1 :(得分:1)

使用Math.pow(1 + t / 100, nbr)代替Math.exp(nbr * t),因为您需要(1+t/100)^nbr(即自1 + t / 100次加nbr次),而不是exp^(nbr*t):< / p>

double pEnd = pStart * Math.pow(1 + t / 100, nbr); // Ending population of user input

答案 2 :(得分:0)

试试这个。

double pEnd = pStart * Math.pow(1.0 + t / 100, nbr);