比较不同利率下的贷款
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();
}
}