gpt4 book ai didi

java - 用java实现摊销程序

转载 作者:太空宇宙 更新时间:2023-11-04 13:59:35 24 4
gpt4 key购买 nike

我需要计算一个人每年支付的贷款利息金额,并显示每年贷款余额的递减情况。我知道如何在纸上获取数字,但我不知道如何为其编写代码,而且其他论坛根本没有帮助。

它应该是什么样子的一个例子是:

Enter loan amount: 90000
Enter loan duration in years: 15
Enter interest rate as a percent: 6.75


For a 15 year loan of $90000.00 at 6.75% interest

Monthly payment = $ 796.42
Total interest = $ 53355.33

Yearly Balances
Year Interest Loan Balance
1 5965.23 86408.21
2 5715.14 82566.33
3 5447.64 78456.94
4 5161.51 74061.43
5 4855.46 69359.87
6 4528.10 64330.94
7 4177.95 58951.87
8 3803.41 53198.26
9 3402.80 47044.03
10 2974.29 40461.31
11 2515.95 33420.24
12 2025.70 25888.91
13 1501.31 17833.19
14 940.40 9216.58
15 340.45 -0.00

What I currently have:

import java.util.*;
public class Loan
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int loanAmount, loanDurationYears, month;
double interestRate, monthlyPayment, totalInterest;

System.out.print("Enter loan amount:");
loanAmount = keyboard.nextInt();

System.out.print("Enter loan duration in years:");
loanDurationYears = keyboard.nextInt();

System.out.print("Enter interest rate as a percent:");
interestRate = keyboard.nextDouble();
keyboard.close();

System.out.println("Loan amount: $" + loanAmount);
System.out.println("Loan duration: " + loanDurationYears + " years");
System.out.println("Interest rate: " + interestRate + "%");

monthlyPayment = payment(loanAmount, loanDurationYears, interestRate);
totalInterest = ((monthlyPayment * (loanDurationYears*12))-loanAmount);
printTotals(loanAmount, loanDurationYears, interestRate, monthlyPayment, totalInterest);
}
public static double payment(int loanAmount, int loanDurationYears, double interestRate)
{
int a = loanAmount;
int n = (loanDurationYears*12);
double i = ((interestRate*.01)/12);
double monthlyPayment = a*(Math.pow((1+i),n)*i)/((Math.pow((1+i),n))-1);
return monthlyPayment;
}
public static void printTotals(int loanAmount, int loanDurationYears, double interestRate, double monthlyPayment, double totalInterest)
{
System.out.println("For a " + loanDurationYears + " year loan of $" + loanAmount + " at " + interestRate + "% interest:");
System.out.printf("Monthly payment = $"+ "%.2f", monthlyPayment);
System.out.println();
System.out.printf("Total interest = $"+ "%.2f", totalInterest);
System.out.println();
}
}

最佳答案

浮点型和 double 型不应用于货币,因为小数部分并不总是精确的。

但是,由于您必须使用 double ,因此我建议您将小数点向右移动一点,直到获得所需的所有精度。

例如。 100.26 不会随时存储在 double 型上,但 100260.00 会存储。这样,您就不会丢失重要的十进制数字“.26”的精度,并在末尾添加精确的额外数字“0”以进行精确舍入(如果需要,否则停止在“26”)。

因为 100.26 绝不能存储在 double 上(否则您将面临失去小数精度的风险),因此您将以字符串或 BigDecimal 类型读取用户输入,并在将数字传递给 double 之前“乘以”1000。如果您使用字符串,您将找到该点,将其删除,并将其放置在右侧 2/3 个点处,然后再转换为 double 。

因为您将乘以 1000,所以您的双最大值将受到更多限制,但如果您不打算使用巨大的数字,这可能不是问题。

您将像平常一样使用 x*1000 进行所有操作,在打印之前,您将把 double 值传递给您的一种精确数据类型、格式以删除 *1000 并打印。

对于这种样式,您不需要专门使用 double ,例如可以使用 long,但是这样,您将获得 double 的精确值。

关于java - 用java实现摊销程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29441293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com