我试图用Java制作汽车贷款利率计算器并且遇到一些奇怪的问题。该计划非常简单,需要输入汽车成本,贷款利率和贷款期限,进行简单的计算,然后输出费率。但是,例如,我正在获得异乎寻常的输出。 1000美元的汽车,5%,4年的贷款,导致每月416.66美元的付款。这显然是不正确的。
import java.util.Scanner;
public class RateCalc {
public static void main(String[] args) {
// /////////////
// Car Price///
// /////////////
Scanner price = new Scanner(System.in);
double carprice;
do {
System.out.println("Please enter car price!");
while (!price.hasNextInt()) {
System.out.println("That's not a number!");
price.next();
}
carprice = price.nextInt();
} while (carprice <= 0);
System.out.println("Thank you! Your car costs $" + carprice + ".");
// /////////////////
// Interest Rate //
// /////////////////
Scanner rate = new Scanner(System.in);
double loanrate;
do {
System.out.println("Please enter your loan rate!");
while (!rate.hasNextInt()) {
System.out.println("That's not a number!");
rate.next();
}
loanrate = rate.nextInt();
} while (loanrate <= 0);
System.out.println("Thank you! Your loan rate is " + loanrate + "%.");
// /////////////////
// Years of Loan //
// /////////////////
Scanner years = new Scanner(System.in);
double yearint;
do {
System.out
.println("Please enter the length of your loan in years!");
while (!years.hasNextInt()) {
System.out.println("That's not a number!");
years.next();
}
yearint = years.nextInt();
} while (yearint <= 0);
System.out.println("Thank you! Your loan is a " + yearint
+ " year loan.");
// /////////////////
// Calculation //
// /////////////////
double monthlypayment;
double top;
double bottom;
double months;
double finalprice;
top = carprice * (loanrate / 12);
bottom = (1 + loanrate / 12);
months = (yearint * 12);
bottom = (int) Math.pow(bottom, -months);
monthlypayment = (top / (1 - bottom));
finalprice = Math.floor(monthlypayment * 100) / 100;
System.out.println("Thank you! Your monthly payment $" +finalprice+ ".");
}
}
答案 0 :(得分:0)
我并不是真的喜欢融资,但我想你需要这样的公式:
double totalAmountToPay = basePrice * Math.pow(1.0 + ratePerMonth, months);
double amountToPayPerMonth = totalAmountToPay / months;
其中,
ratePerMonth
是双倍的。例如:3%应为:0.03。basePrice
是汽车的价格,即:10000。months
是一个整数。答案 1 :(得分:0)
$ 416.66大致适合4年1000美元的500%贷款。考虑到你在几个地方将速率加到1.0,我认为你的代码期望小数速率(5%= 0.05)而不是整数速率(5%= 5)。如果您将费率输入除以100,我认为您将得到您正在寻找的答案。
答案 2 :(得分:0)
如果这很简单,那么您使用的公式是错误的...... 试试这个
double total = carprice * (1.0 + (loanrate / 100.0) * yearint);
double monthlypayment = total / (yearint * 12);
System.out.println("Thank you! Your monthly payment $" +monthlypayment+ ".");
使用简单利息,4年期贷款中的1000(期限= 1年),5%的利率是1200 ...每月付款变为25美元......