Java 使用复合增长的程序

Java program using compound growth

程序简介如下:"According to CNNMoney.com, Facebook hit 1 billion users in October 2012. Using the compound-growth technique you learned in Fig 4.6 and assuming its user base grows at a rate of 4% per month, how many months will it take to grow its user base to 1.5 billion users? How many months will it take for Facebook to grow its user base to 2 billion users?"

我有程序 "working",但我觉得它好像没有正确计算值。我也不知道我使用的是最新的还是最有效的 Java 技术。我绝对是初学者,所以任何建议或指示将不胜感激。这是我目前所拥有的!

package programmingassignment4.pkg32;

public class ProgrammingAssignment432 {

    public static void main(String[] args) {
        int user= 1000000000;
        int user2= 1000000000;
        double total1 = 0;
        double total2 = 0;
        int grandtotal1=0;
        int grandtotal2=0;
        int i = 1;
        int j=1;
        for(i=1; user<=1500000000; i++)
        {
           total1 = user*(.04);
           user+=total1;
        }
        System.out.print("The total number of months to reach 1.5 billion users will be : " + i + "\n");
        for(j=1;user2<=2000000000;j++)
        {
            total2 = user2*(.04);
            user2+=total2;
        }
        System.out.print("The total number of months to reach 2 billion users will be : " + j);
   }
}

我会说你的代码是在正确的轨道上,而且你似乎理解这个概念。但是,我相信您的 for 循环会错误地为您留下一个比达到目标所需的实际月数大 1 的值,因为您从 1 而不是 0 开始。

在循环之前声明循环变量也有点奇怪(但没有错),所以我想我应该改用 while 循环。此外,因为您实际上 运行 两次相同的逻辑,所以最好将代码移到它自己的方法中,以便您可以灵活地调用它。像这样:

private static int monthsOfGrowthRequired(int startAmount,
        int targetAmount, double growthFactor) {
    int monthsOfGrowth = 0;
    int runningAmount = startAmount;
    while (runningAmount < targetAmount) {
        runningAmount *= growthFactor;
        ++monthsOfGrowth;
    }
    return monthsOfGrowth;
}

请注意,对于每月 +4% 的增长率,此方法将采用 growthFactor 值 1.04。 (因此对于每月 -4% 的利率,值为 0.96。)

至于检查您的号码:那很容易。由于可以直接使用对数,因此无需借助像这样的数值分析方法就可以计算出确切的数字。比如目标是15亿,其中m等于需要增长的月数,可以写出这个等式:

1.04 ^ m = 1.5

因此:

m = (log 1.5) / (log 1.04)

您可以将相同的逻辑应用于目标为 20 亿的情况,并改进建议的方法以添加可能需要的任何安全检查以避免错误输入并捕获任何情况方法永远不会结束。

我希望我的回答仍然能帮助任何正在寻找这个问题的解决方案的人。我认为这里 posted 的唯一解决方案有点复杂。试试我的。

你的作业说要使用复合增长公式,那你为什么不用呢?

a = p(1 + r)n

其中 a 是总量,p 是您的起始率(在本例中为 10 亿),r 是您的增长率,n 是月数。非常直接。

您可以使用Math.pow Java 方法。

total = startrate * Math.pow(1.0 + growthrate, month);

Math.pow(x, y) 计算 xyth 次方的值。这 方法接收两个双参数和 returns 一个 double 值。

这是我的 运行 代码的输出:

    Facebook User Base Growth

Projected User Base in months after October 2012
   1    1,040,000,000.00
Months
   2    1,081,600,000.00
Months
   3    1,124,864,000.00
Months
   4    1,169,858,560.00
Months
   5    1,216,652,902.40
Months
   6    1,265,319,018.50
Months
   7    1,315,931,779.24
Months
   8    1,368,569,050.41
Months
   9    1,423,311,812.42
Months
  10    1,480,244,284.92
Months
  11    1,539,454,056.32
Months

It took Facebook 11 months to grow that big.

Now we will count how many months it will take to get to 2bln users.

  11    1,539,454,056.32
Months
  12    1,601,032,218.57
Months
  13    1,665,073,507.31
Months
  14    1,731,676,447.60
Months
  15    1,800,943,505.51
Months
  16    1,872,981,245.73
Months
  17    1,947,900,495.56
Months
  18    2,025,816,515.38
Months

It took Facebook 18 months to grow that big.

如果它引起了足够的兴趣,我可以 post 在这里完整的代码。 编程愉快!