百万富翁-循环练习

假设你月收入是3000,除开平时花销,每个月留下1000块钱进行投资。

然后你认真的钻研了 《股票和基金 21天从入门到精通》,达到了每年20%的投资回报率。

那么问题来了,以每个月投资1000块钱的节奏,持续投资多少年,总收入达到100万
(复利计算按照每年12000投入计算,不按照每月计息)

复利公式:
F = p* ( (1+r)^n );
F 最终收入
p 本金
r 年利率
n 存了多少年

while

public class QiGai {
	public static void main(String[] args) {
		int F = 1000000;
		int p = 12000;
		double r = 0.2;
		int n=1;
		
		double x = p*(1+r);
		while( x < F) {
			n++;
			System.out.println("截止到第"+n+"年初总收入为"+x);
			x = (x+p)*(1+r);
			if(x >=F){
        System.out.println("截止到第"+(n+1)+"年初总收入为"+x);
        break;
			}
		} 
		
		
		
	}
}
/*
截止到第2年初总收入为14400.0
截止到第3年初总收入为31680.0
截止到第4年初总收入为52416.0
截止到第5年初总收入为77299.2
截止到第6年初总收入为107159.04
截止到第7年初总收入为142990.848
截止到第8年初总收入为185989.0176
截止到第9年初总收入为237586.82111999998
截止到第10年初总收入为299504.18534399994
截止到第11年初总收入为373805.02241279994
截止到第12年初总收入为462966.0268953599
截止到第13年初总收入为569959.2322744319
截止到第14年初总收入为698351.0787293182
截止到第15年初总收入为852421.2944751818
截止到第16年初总收入为1037305.5533702181*/

 for

public class Millionaire {

	public static void main(String[] args) {
		// 采用秦九韶算法
		int year;
		double sum=12000*1.2;
		for(year=2;sum<1000000;year++)
		{
			sum=(sum+12000)*1.2;
		}
		System.out.println("持续投资"+(year-1)+"年,收入达到100万。");
	}
}
原文地址:https://www.cnblogs.com/lsswudi/p/11294463.html