⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercise3_26.java

📁 Introduction to java programming 一书中所有编程练习部分的源码
💻 JAVA
字号:
//Exercise3_26.java: displays the monthly and total payments
//for each interest rate starting from 5% to 8%,
//with an incremental of 1/8.
import javax.swing.JOptionPane;

public class Exercise3_26 {
  // Main method
  public static void main(String[] args) {
    int numOfYears;
    double loanAmount;

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

    // Convert string to int
    numOfYears = Integer.parseInt(numOfYearsString);
    
    // Enter loan amount
    String loanString = JOptionPane.showInputDialog(null, 
      "Enter loan amount, for example 120000.95:",
      "Exercise3_26 Input", JOptionPane.QUESTION_MESSAGE);

    // Convert string to double
    loanAmount =  Double.parseDouble(loanString);

    // Display the header
    System.out.print("Interest Rate");
    System.out.print("\t" + "Monthly Payment");
    System.out.println("\t\t" + "Total Payment");

    double monthlyInterestRate;
    double monthlyPayment;
    double totalPayment;

    for (double annualInterestRate = 5.0; annualInterestRate <= 8.0;
      annualInterestRate += 1.0 / 8) {
      // Obtain monthly interest rate
      monthlyInterestRate = annualInterestRate / 1200;

      // Compute mortgage
      monthlyPayment = loanAmount*monthlyInterestRate/
        (1 - (Math.pow(1 / (1 + monthlyInterestRate), numOfYears * 12)));
      totalPayment = monthlyPayment * numOfYears * 12;

      // Display results
      System.out.print(annualInterestRate + "%");
      System.out.print("\t\t" + (int)(monthlyPayment * 100) / 100.0);
      System.out.println("\t\t\t" + (int)(totalPayment * 100) / 100.0);
    }
    
    System.exit(0);
  }
}

⌨️ 快捷键说明

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