Java黑皮书课后题第2章:*2.21(金融应用:计算未来投资回报)编写程序,读取投资总额、年利率和年龄,显示未来投资回报金额
生活随笔
收集整理的這篇文章主要介紹了
Java黑皮书课后题第2章:*2.21(金融应用:计算未来投资回报)编写程序,读取投资总额、年利率和年龄,显示未来投资回报金额
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
*2.21(金融應用:計算未來投資回報)編寫程序,讀取投資總額、年利率和年齡,顯示未來投資回報金額
- 題目
- 題目概述
- 舉例與運行示例
- 思路
- 代碼
- 贅述
題目
題目概述
2.21(金融應用:計算未來投資回報)編寫程序,讀取投資總額、年利率和年齡,顯示未來投資回報金額
未來投資回報金額 = 投資金額 * Math.pow((1 + 月利率), 年數12)
上式簡化為:futureValue = investmentAmount * Math.pow((1 + rate), years * 12)
舉例與運行示例
如果輸入的投資金額為1000,年利率為3.25%,年數為1,那么未來投資回報金額為1032.98
Enter investment amount: 1000.56
Enter annual interest rate in percentage: 4.25
Enter number of years: 1
Future value is $1043.92
思路
如果看過上一篇(2.20計算利息)的同學應該就知道這道題怎么做了
但是注意輸入的annual interest rate > 1,賦值時要除100
★★★★★
特別注意:輸入的rate是年利率,但公式中給出的是要使用月利率
★★★★★
代碼
import java.util.Scanner;public class Test2_21 {public static void main(String[] args) {// 獲取各種數據Scanner input = new Scanner(System.in);System.out.println("Enter investment amount: ");double investmentAmount = input.nextDouble();System.out.println("Enter annual interest rate in percentage: ");double rate = input.nextDouble() / (100.0 * 12);System.out.println("Enter number of years: ");int years = input.nextInt();// 計算double futureValue = investmentAmount * Math.pow((1 + rate), years * 12);System.out.println("Future value is $" + futureValue);} }贅述
這道題確實坑很多,也是改了好幾次,剛開始寫報錯很正常
總結
以上是生活随笔為你收集整理的Java黑皮书课后题第2章:*2.21(金融应用:计算未来投资回报)编写程序,读取投资总额、年利率和年龄,显示未来投资回报金额的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java黑皮书课后题第2章:*2.20(
- 下一篇: Java黑皮书课后题第2章:*2.22(