Home  >  Article  >  Java  >  Sample code sharing on how to use counters in Java to solve a population growth problem

Sample code sharing on how to use counters in Java to solve a population growth problem

黄舟
黄舟Original
2017-07-24 13:14:311354browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn