exercise4_3.java
来自「java程序设计 机械工业出版社 书籍代码」· Java 代码 · 共 39 行
JAVA
39 行
// Exercise4_3.java: Create a method for computing future value
import javax.swing.JOptionPane;
public class Exercise4_3 {
public static void main(String[] args) {
// Enter yearly interest rate
String annualIntrestRateString = JOptionPane.showInputDialog(
null, "Enter yearly interest rate, for example 8.25:",
"Exercise 4_3 Input", JOptionPane.QUESTION_MESSAGE);
// Convert string to double
double annualInterestRate =
Double.parseDouble(annualIntrestRateString);
// Enter loan amount
String loanString = JOptionPane.showInputDialog(null,
"Enter loan amount, for example 120000.95:",
"Exercise 4_3 Input", JOptionPane.QUESTION_MESSAGE);
// Convert string to double
double loanAmount = Double.parseDouble(loanString);
System.out.println("Years\tFuture Value");
for (int i = 1; i <= 30; i++) {
double futureValue =
futureInvestmentValue(loanAmount, annualInterestRate / 1200, i);
System.out.println("" + i + "\t" + (int)(futureValue * 100) / 100.0);
}
System.exit(0);
}
public static double futureInvestmentValue(double investmentAmount,
double monthlyInterestRate, int numOfYears) {
return investmentAmount * Math.pow(1 + monthlyInterestRate,
numOfYears * 12);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?