比较不同利率下的贷款
作者:互联网
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();
}
}
标签:Scanner,int,System,input,print,贷款,比较,利率,out 来源: https://www.cnblogs.com/doudou-20123/p/16309419.html