第五章第三十一题(金融应用:计算CD价值)(Financial application: compute CD value)
-
*5.31(金融应用:计算CD价值)假设你用10000美元投资一张CD,年获利率为5.75%。
一个月后,这张CD价值为 10000 + 10000 * 5.75 / 1200 = 10047.92
两个月之后,这张CD价值为10047.92 + 10047.92 * 5.75 / 1200 = 10096.06
三个月之后,这张CD价值为10096.06 + 10096.06 * 5.75 / 1200 = 10144.44
以此类推。
编写程序,提示用户输入一个金额数(例如:10000)、年获利率(例如:5.75)以及月份数(例如18),然后显示一个表格,如下面的运行示例所示:
Enter the initial deposit amount: 10000
Enter annual percentage yield: 5.75
Enter maturity period (number of months): 18
Month CD Value
1 10047.92
2 10096.06
…
17 10846.57
18 10898.54
*5.31(Financial application: compute CD value) Suppose you put $10,000 into a CD with an annual percentage yield of 5.75%.
After one month, the CD is worth 10000 + 10000 * 5.75 / 1200 = 10047.92
After two months, the CD is worth 10047.92 + 10047.92 * 5.75 / 1200 = 10096.06
After three months, the CD is worth 10096.06 + 10096.06 * 5.75 / 1200 = 10144.44
and so on.
Write a program that prompts the user to enter an amount (e.g., 10000), the annual percentage yield (e.g., 5.75), and the number of months (e.g., 18) and displays a table as shown in the sample run.
Enter the initial deposit amount: 10000
Enter annual percentage yield: 5.75
Enter maturity period (number of months): 1
Month CD Value
1 10047.92
2 10096.06
…
17 10846.57
18 10898.54 -
参考代码:
package chapter05;
import java.util.Scanner;
public class Code_31 {
public static void main(String[] args) {
double amount, annualPercentageYield;
int numberOfMonths;
Scanner inputScanner = new Scanner(System.in);
System.out.print("Enter the initial deposit amount: ");
amount = inputScanner.nextDouble();
System.out.print("Enter annual percentage yield: ");
annualPercentageYield = inputScanner.nextDouble();
System.out.print("Enter maturity period (number of months): ");
numberOfMonths = inputScanner.nextInt();
System.out.println("Month\tCD Value");
for(int i = 1;i <= numberOfMonths;i++) {
amount = amount + amount * annualPercentageYield / 1200;
System.out.printf("%d\t%.2f\n", i, amount);
}
}
}
- 结果显示:
Enter the initial deposit amount: 10000
Enter annual percentage yield: 5.75
Enter maturity period (number of months): 18
Month CD Value
1 10047.92
2 10096.06
3 10144.44
4 10193.05
5 10241.89
6 10290.97
7 10340.28
8 10389.82
9 10439.61
10 10489.63
11 10539.89
12 10590.40
13 10641.14
14 10692.13
15 10743.37
16 10794.84
17 10846.57
18 10898.54
Process finished with exit code 0