java问题求解答 本金1000元 年利率0.05 每年都存进2000元,要多少年才能有一百万?

如题所述

第1个回答  2013-10-08
public static void main(String[] args) {
double number=1000;//本金
for (int i = 0; i < 1000000; i++) {//用这种极端算法。假设需要100万年才可以获得100万
number+=number*0.05+2000;//每年本金加利息再加上每年多存2000元作为下一年的本金算
if(number>=1000000){//当本金大于或等于一百万的时候输出年份和数目
System.out.println(i);//要多少年
System.out.println(number);//有多少钱
break;//退出循环
}
}
}本回答被网友采纳
第2个回答  2013-10-08
public class Test {
    public static void main(String[] args) {
double principal = 1000;
int yearCount = 0;
while(principal < 1000000){
principal = 1.05 * principal +2000;
yearCount++;
System.out.println("第"+yearCount+"年,资金:"+principal);
}
System.out.println("需要"+yearCount+"年可以有一百万。");
}
}

第3个回答  2013-10-08
public class Test {

static int year =0;      //record the year 
static double amount=0;  //the total money you have at the end of year
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
calculate(1000*1.05);
System.out.println("According to what you said ,the year needed is "+(year+1));
}


public static void calculate(double total){
if(total>=1000000)
return;
amount = (2000+total)*1.05;
year++;
if(amount>=1000000)
return;
else
calculate(amount);
}
}

 


我的用递归有点复杂,楼主参考楼上的就好。

第4个回答  2013-10-08
public class Test {
private static int year = 0;
private static double sum = 1000 + 1000 * 0.05;

public static void main(String[] args) {
for (int i = 1; i <= 100000; i++) {
sum += sum * 0.05 + 2000;
if (sum >= 1000000) {
System.out.println(year);
break;
}
year++;
}
}
}

第5个回答  2013-10-08
67年,不过这跟java有什么关系。。。
相似回答