有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

eclipse在数学公式Java方面遇到了麻烦

我正在为我的java类制作一个程序,它计算一年的人口数量,并以每年1.2%的速度增长。2011年的人口是7000(我用的是小数,而不是数十亿)。我现在有这个代码

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年底时,我得到22824。我试过用谷歌搜索一个公式,但什么也找不到。你们有谁知道这个公式吗?如果你以2016年为年底,这个数字应该在7.433左右


共 (1) 个答案

  1. # 1 楼答案

    使用Math.pow(1 + t / 100, nbr)而不是Math.exp(nbr * t),因为您需要(1+t/100)^nbr(即将1 + t / 100自身乘以nbr),而不是exp^(nbr*t)

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