How to use counters in Java to solve a population growth problem sample code sharing
/** * Created by liwenj on 2017/7/17. * 问题:某学校现今人数25万,以每年25%的增长速度,多久可到达100万? */ public class test11 { public static void main(String[] args) { //i为计数器,存储循环的次数 int i=0; double num=25; int year=2012; double speed=0.25; while(num<100){ num=num*(1+speed); i++; } System.out.println("到"+(year+i)+"年,人数到达100万"); } }
The above is the detailed content of Sample code sharing on how to use counters in Java to solve a population growth problem. For more information, please follow other related articles on the PHP Chinese website!