比较不同利率下的贷款
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入贷款总额,比如 300000:");
double loanAmount = input.nextDouble();
System.out.print("请输入年数,比如 5:");
int numberOfYears = input.nextInt();
double annualInterestRate = 5.0;
for(int i = 0; i <= 24;i++){
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate /
(1 - 1/Math.pow(1 + monthlyInterestRate,numberOfYears * 12));
double totalPayment = monthlyPayment * numberOfYears * 12;
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;
double monthlyPayment = loanAmount * monthlyInterestRate /
(1 - 1/Math.pow(1 + monthlyInterestRate,numberOfYears * 12));
double totalPayment = monthlyPayment * numberOfYears * 12;
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();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律