第五章第三十题(金融应用:复利值)(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);
}
}
java

- 结果显示:
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
java
