第五章第三十题(金融应用:复利值)(Financial application: compound value)

第五章第三十题(金融应用:复利值)(Financial application: compound value)

  • *5.30(金融应用:复利值)假设你每月在储蓄账户上存100美元,年利率是5%。那么每月利率是0.05 / 12 = 0.00417。
    在第一月之后,账户上的值变成:
    100 * (1 + 0.00417) = 100.417
    第二个月之后,账户上的值变成:
    (100 + 100.417) * (1 + 0.00417) = 201.252
    第三个月之后,账户上的值变成:
    (100 + 201.252) * (1 + 0.00417) = 302.507
    以此类推。
    编写程序提示用户输入一个金额数(例如:100)、年利率(例如:5)以及月份数(例如:6),然后显示给定月份后账户上的钱数。
    *5.30(Financial application: compound value) Suppose you save $100 each month in a savings account with annual interest rate 5%. The monthly interest rate is 0.05 / 12 = 0.00417.
    After the first month, the value in the account becomes
    100 * (1 + 0.00417) = 100.417
    After the second month, the value in the account becomes
    (100 + 100.417) * (1 + 0.00417) = 201.252
    After the third month, the value in the account becomes
    (100 + 201.252) * (1 + 0.00417) = 302.507
    and so on.
    Write a program that prompts the user to enter an amount (e.g., 100), the annual interest rate (e.g., 5), and the number of months (e.g., 6) and displays the amount in the savings account after the given month.
  • 参考代码:
package chapter05;

import java.util.Scanner;

public class Code_30 {
    public static void main(String[] args) {
        double amount, annualInterestRate, numberOfMonths, account = 0;
        Scanner inputScanner = new Scanner(System.in);
        System.out.print("Enter an amount: ");
        amount = inputScanner.nextDouble();
        System.out.print("Enter the annual interest rate (e.g., 3.75): ");
        annualInterestRate = inputScanner.nextDouble();
        System.out.print("Enter the number of months: ");
        numberOfMonths = inputScanner.nextDouble();
        for(int i = 1;i <= numberOfMonths;i++)
            account = (amount + account) * (1 + (annualInterestRate/ 100.0 / 12.0));
        System.out.printf("Your account is %.3f", account);
    }
}

  • 结果显示:
Enter an amount: 100
Enter the annual interest rate (e.g., 3.75): 5
Enter the number of months: 6
Your account is 608.811
Process finished with exit code 0

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值