package test;
import java.util.*;
public class Retirement//退休
{
private static Scanner in;
public static void main(String[] args)
{
in = new Scanner(System.in);
System.out.println("How much money dou you need to retire?");
double goal=in.nextDouble();
System.out.print("Hou much money will you contribute every year?");
double payment=in.nextDouble();
System.out.print(" Interst rate in %:");
double interestRate=in.nextDouble();
double balance=0;
int years=0;
//update account balance while goal is't reached 更新帐户余额而目标是没有达到
while (balance<goal)
{
//add this year's payment and interst
balance+=payment;
double interst = balance * interestRate / 100;
balance+=interst;
years++;
}
System.out.println("You can retire in "+years+"years.");
}
}
转载于:https://my.oschina.net/javwang/blog/656164