从现在开始,你如果能够定期每年存下15000元,如果你每年存下的钱都能投资到股票或房地产,并获得每年平均20%的投资回报率,如此持续40年后,你能积累多少财富呢? 请看下面的计算结果: 具体实现代码如下: import java.util.Scanner; public class test { /** * @param args */ private double sum; public double test(double vaule,int n){//定义方法 for(int i=1;i<> sum=sum+Math.pow(1+vaule, i); // 计算收益。pow为平方 } return sum; } public static void main(String[] args) { while(true){ System.out.println('投资年限:'); Scanner sc = new Scanner(System.in); //控制台输入 Integer aa=sc.nextInt();//接受控制台输入 if(aa!=null){ System.out.println('年投资金额:'); Scanner sc1 = new Scanner(System.in); //控制台输入 Integer bb=sc1.nextInt(); if(bb!=null){ System.out.println('你是收益是:'); Scanner sc2 = new Scanner(System.in); //控制台输入 double bb1=sc2.nextDouble(); test test=new test();//定义对象 double yyy= test.test(bb1,aa);//调用函数 int yyyy= (int) (yyy*bb);//算出最后结果 System.out.println('最终收益:'+yyyy);//打印输出 System.out.println(''); } } } } } |
|