第一章第十一题(人口估算)(Population projection)

第一章第十一题(人口估算)(Population projection)

  • *1.11(人口估算)美国人口调查局基于以下假设进行人口估算:
    • 每7秒有一个人诞生
    • 每13秒有一个人死亡
    • 每45秒有一个移民迁入

编写一个程序,显示未来5年的每年人口数。假设当前的人口是312 032 486,每年有365天。提示:Java中,两个整数相除,结果还是整数,小数部分被去掉。例如,5/4等于1(而不是1.25),10/4等于2(而不是2.5)。如果想得到有小数部分的精确结果,进行除法运算的两个值之一必须是一个具有小数点的数值。例如,5.0/4等于1.25,10/4.0等于2.5。

  • *1.11 (Population projection) The U.S. Census Bureau projects population based on the following assumptions:
    • One birth every 7 seconds
    • One death every 13 seconds
    • One new immigrant every 45 seconds

Write a program to display the population for each of the next five years. Assume that the current population is 312,032,486, and one year has 365 days. Hint: In Java, if two integers perform division, the result is an integer. The fractional part is truncated. For example, 5 / 4 is 1 (not 1.25) and 10 / 4 is 2 (not 2.5). To get an accurate result with the fractional part, one of the values involved in the division must be a number with a decimal point. For example, 5.0 / 4 is 1.25 and 10 / 4.0 is 2.5.

  • 参考代码:
package chapter01;

public class Code_11 {
    public static void main(String[] args) {

        final double BORN_NUMBER = 1.0 / 7; 		// Births per second
        final double DEAD_NUMBER = 1.0 / 13;		// Deaths per second
        final double IMMIGRANT_NUMBER = 1.0 / 45;	// Number of immigrants per second
        final int SECONDS_IN_A_YEAR = 365 * 24 * 60 * 60; // Seconds in a year

        int PopulationNumberNow = 312_032_486;
        int FirstYearPopulation =(int)(PopulationNumberNow + SECONDS_IN_A_YEAR * BORN_NUMBER - SECONDS_IN_A_YEAR * DEAD_NUMBER + SECONDS_IN_A_YEAR * IMMIGRANT_NUMBER);
        int SecondYearPopulation = (int)(FirstYearPopulation + SECONDS_IN_A_YEAR * BORN_NUMBER - SECONDS_IN_A_YEAR * DEAD_NUMBER + SECONDS_IN_A_YEAR * IMMIGRANT_NUMBER);;
        int ThirdYearPopulation = (int)(SecondYearPopulation + SECONDS_IN_A_YEAR * BORN_NUMBER - SECONDS_IN_A_YEAR * DEAD_NUMBER + SECONDS_IN_A_YEAR * IMMIGRANT_NUMBER);;
        int FourthYearPopulation = (int)(ThirdYearPopulation + SECONDS_IN_A_YEAR * BORN_NUMBER - SECONDS_IN_A_YEAR * DEAD_NUMBER + SECONDS_IN_A_YEAR * IMMIGRANT_NUMBER);;
        int FifthYearPopulation = (int)(FourthYearPopulation + SECONDS_IN_A_YEAR * BORN_NUMBER - SECONDS_IN_A_YEAR * DEAD_NUMBER + SECONDS_IN_A_YEAR * IMMIGRANT_NUMBER);;

        System.out.println("The population number of USA in first year : " + FirstYearPopulation);
        System.out.println("The population number of USA in second year : " + SecondYearPopulation);
        System.out.println("The population number of USA in third year : " + ThirdYearPopulation);
        System.out.println("The population number of USA in fourth year : " + FourthYearPopulation);
        System.out.println("The population number of USA in fifth year : " + FifthYearPopulation);
    }
}

  • 结果显示:
The population number of USA in first year : 314812582
The population number of USA in second year : 317592678
The population number of USA in third year : 320372774
The population number of USA in fourth year : 323152870
The population number of USA in fifth year : 325932966

Process finished with exit code 0

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值