testifelse.java

来自「此源码为机械工业出版社出版的《Java语言程序设计》第三版所配套的书中所有源代码」· Java 代码 · 共 46 行

JAVA
46
字号
// TestIfElse.java: Test if-else statements
public class TestIfElse
{
  // Main method
  public static void main(String[] args)
  {
    double annualInterestRate = 0;
    int numOfYears;
    double loanAmount;

    // Enter number of years
    System.out.print(
      "Enter number of years (7, 15 and 30 only): ");
    numOfYears = MyInput.readInt();

    // Find interest rate based on year
    if (numOfYears == 7)
      annualInterestRate = 7.25;
    else if (numOfYears == 15)
      annualInterestRate = 8.50;
    else if (numOfYears == 30)
      annualInterestRate = 9.0;
    else
    {
      System.out.println("Wrong number of years");
      System.exit(0);
    }

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

    // Enter loan amount
    System.out.print("Enter loan amount, for example 120000.95: ");
    loanAmount = MyInput.readDouble();

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

    // Display results
    System.out.println("The monthly payment is " + monthlyPayment);
    System.out.println("The total payment is " + totalPayment);
  }
}

⌨️ 快捷键说明

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