# 比较不同利率下的贷款# 显示分期还贷时间表

比较不同利率下的贷款

import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    //Create a Scanner
    Scanner input = new Scanner(System.in);
    
    //Enter loan amount
    System.out.print("请输入贷款总额,比如 300000:");
    double loanAmount = input.nextDouble(); 
    
    //Enter number of years
    System.out.print("请输入年数,比如 5:");
    int numberOfYears = input.nextInt();
       
    double annualInterestRate = 5.0;

    for(int i = 0; i <= 24;i++){
      //Obtain monthly interest rate
      double monthlyInterestRate = annualInterestRate / 1200;

      //Calculate payment
      double monthlyPayment = loanAmount * monthlyInterestRate / 
      (1 - 1/Math.pow(1 + monthlyInterestRate,numberOfYears * 12));
      double totalPayment = monthlyPayment * numberOfYears * 12;
      
      //Display results
      if(i == 0)
        System.out.print("Interest Rate       Monthly Payment     Total Payment\n");
      
      System.out.print(annualInterestRate + "%\t\t\t\t");
      System.out.printf("%-20.2f%-10.2f\n", monthlyPayment, totalPayment);
      
      annualInterestRate += 0.125;
     }
    input.close();
  }
}

显示分期还贷时间表

import java.util.Scanner;
class Main {
  public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    
    System.out.print("Loan Amount: ");
    double loanAmount = input.nextDouble();
    
    System.out.print("Number of Years: ");
    int numberOfYears = input.nextInt();
    
    
    System.out.print("Annual Interest Rate: ");
    double annualInterestRate = input.nextDouble();
    double monthlyInterestRate = annualInterestRate / 1200;

    //Calculate payment
    double monthlyPayment = loanAmount * monthlyInterestRate / 
      (1 - 1/Math.pow(1 + monthlyInterestRate,numberOfYears * 12));
    double totalPayment = monthlyPayment * numberOfYears * 12;
    
    //Display results
    System.out.println("\nMonthly Payment: " + (int)(monthlyPayment * 100) / 100.0);
    System.out.println("Total Payment: " + (int)(totalPayment * 100) / 100.0);
    monthlyPayment = (int)(monthlyPayment * 100) / 100.0;

    double interest = 0;
    double balance = loanAmount;
    double principal = 0;
       
    System.out.println("\nPayment#\tInterest\tPrincipal\tBalance");
    for(int i = 1; i <= numberOfYears * 12; i++){
      interest =   balance * monthlyInterestRate;
      
      interest = (int)(interest * 100) / 100.0;
      
      principal = monthlyPayment - interest;
      
      balance = balance - principal;
      balance = (int)(balance * 100) / 100.0;
      System.out.println(i + "\t\t\t" + interest + "\t\t" + principal + "\t\t" + balance);
   }
    input.close();
  }
}
posted @   Scenery_Shelley  阅读(40)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示