62,614
社区成员
发帖
与我相关
我的任务
分享
/*
* 计算任意月的连本带利的值
* 比如月利率是r,本金是a,月数为m,连本带利的和为x
* 则第一个月后 x = a(1+r);
* 第二个月后x = (a(1+r)+a)(1+r)= a[(1+r)^2 + (1+r)];
* 第三个月后x = [a[(1+r)^2 + (1+r)]](1+r) = a[(1+r)^3 + (1+r)^2 + (1+r)];
* 第m个月后x = a[(1+r)^m + (1+r)^(m-1) + (1+r)^(m-2) + .... + ()]
* = a[(1+r)((1+r)^m-1)]/r
* 最后这一步利用等比数列求值公式。
* 把 amount--> a, monthlyInterestRate-->r, monthOfNumbers--> m替换后就是代码的公式。
*/
double allMoney = amount * (1 + monthlyInterestRate) * (Math.pow(1 + monthlyInterestRate, monthOfNumbers) - 1) / monthlyInterestRate;
import java.util.Scanner;
public class CalculationCompoundInterest {
public static void main(String[] args) {
double YEAR_RATE = 0.05;
double monthRate = YEAR_RATE/12;
System.out.println("Please input your money:");
Scanner input = new Scanner(System.in);
double money = input.nextDouble();
System.out.println("How many months do you want to store?");
int month = input.nextInt();
double totalMoney = 0.0;
for(int i = 1;i<=month;i++){
totalMoney = (money+totalMoney)*(1+monthRate);
}
System.out.printf("After%2d months,your total money will be:$%7.3f", month,totalMoney);
input.close();
}
}
public class Demo {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("no args.");
}
}
}