exercise2_12.java

来自「java程序设计导论(daniel liang著) 所有偶数课后习题答案」· Java 代码 · 共 42 行

JAVA
42
字号
// Exercise2_12.java: Create a method for computing future value
import javax.swing.JOptionPane;

public class Exercise2_12 {
  public static void main(String[] args) {
    // Enter yearly interest rate
    String annualIntrestRateString = JOptionPane.showInputDialog(
      "Enter yearly interest rate, for example 8.25:");

    // Convert string to double
    double annualInterestRate =
      Double.parseDouble(annualIntrestRateString);

    // Obtain monthly interest rate
    double monthlyInterestRate = annualInterestRate/1200;

    // Enter number of years
    String numOfYearsString = JOptionPane.showInputDialog(
      "Enter number of years as an integer, \nfor example 5:");

    // Convert string to int
    int numOfYears = Integer.parseInt(numOfYearsString);

    // Enter the investment amount
    String investmentAmountString = JOptionPane.showInputDialog(
      "Enter the investment amount, for example 120000.95:");

    // Convert string to double
    double investmentAmount =  Double.parseDouble(investmentAmountString);

    double futureValue =
      investmentAmount * Math.pow(1 + monthlyInterestRate,
      numOfYears * 12);

    System.out.print("Future value is " +
      (int)(futureValue * 100) / 100.0);

    System.exit(0);
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?