Java、计算未来投资值

编写程序,读取投资总额、年利率和年数,然后使用下面的公式显示未来投资金额:

                         未来投资金额 = 投资总额 * (1 + 月利率)^(年数 * 12)


package pack2;

import java.util.Scanner;

public class AccumulatedValue {

	public static void main(String[] args) {
		try(Scanner input = new Scanner(System.in);) {
			System.out.print("Enter investment amount: ");
			double investment = input.nextDouble();
			
			System.out.print("Enter annual interest rate in percentage: ");
			double annualInterest = input.nextDouble();
			
			System.out.print("Enter number of years: ");
			double years = input.nextDouble();
			
			System.out.printf("Accumulated value is $%.2f\n", 
                     accumulatedValue(investment, annualInterest, years));
		}
	}
	
	//未来投资金额 = 投资总额 * (1 + 月利率)^(年数 * 12)
	public static double accumulatedValue(double investment, 
                            double annualInterest, double years) {
		double monthlyInterestRate = annualInterest / 1200;
		return investment * Math.pow(1 + monthlyInterestRate, years * 12);
	}

}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值