利息计算器中的逻辑错误 [英] Logic Error in Interest Calculator
本文介绍了利息计算器中的逻辑错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
MAIN:
package loantable;
import java.util.ArrayList;
public class LoanTable
{
private double loanAmount;
private double loanLength;
private double lowInterest;
private double highInterest;
private int count;
ArrayList< Double> payments = new ArrayList<>();
public LoanTable( double loanAmount, double loanLength, double lowInterest, double highInterest)
{
this .loanAmount = loanAmount;
this .loanLength = loanLength;
this .lowInterest = lowInterest;
this .highInterest = highInterest;
}
public ArrayList getInterestRates()
{
count = - 1 跨度>;
while (lowInterest< = highInterest)
{
double monthlyInterest = lowInterest / 12 。 0 ;
double length = loanLength * 12 ;
double c = Math.pow(( 1 + monthlyInterest),length);
double monthlyPayment =(loanAmount * monthlyInterest * c)/(c - 1 );
payments.add(monthlyPayment);
lowInterest + =。 25 ;
count ++;
}
返回付款;
}
public String to()
{
字符串 theory = ;
double currentInterest = highInterest;
ArrayList< Double> t = new ArrayList<>();
t = getInterestRates();
while (count> = 0 )
{
theory = theory + \ n + 利率: + currentInterest + 每月付款: + t.get(count);
count--;
currentInterest - =。 25 ;
}
return 理论;
}
}
TESTER:
package loantable;
import java.util.Scanner;
public class LoanTableTester
{
public static void main( String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.print( Principal:);
double loanAmount = sc.nextDouble();
System.out.print( \ nTime:);
double time = sc.nextDouble();
System.out.println( \ n低利率:);
double lowRate = sc.nextDouble();
System.out.println( \ nHigh Rate:);
double highRate = sc.nextDouble();
LoanTable t = new LoanTable(loanAmount,time,lowRate,highRate);
System.out.println(t.to());
}
}
CONSOLE:
校长:100000
时间:30
低利率:
11
高利率:
12
利率:12.0每月付款:100000.0
利率:11.75每月付款: 97916.66666666666
利率:11.5每月付款:95833.33333333334
利率:11.25每月付款:93750.0
利率:11.0每月付款:91666.66666666664
正确的回答:
<前郎=文本>校长= $ 100000.00
时间= 30年
低利率= 11%
高利率= 12%
11.00 952.32
11.25 971.26
11.50 990.29
11.75 1009.41
12.00 1028.61
公式:
确定付款的公式为:
(p * k * c)/(c - 1)
p =本金,借入金额
k =月利率(年利率/ 12.0)
n = mo的数量nthly付款(年* 12)
c =(1 + k)^ n
a =每月付款(支付利息和本金)
>
我已经尝试了我能想到的一切,不知道是什么导致了这个逻辑问题!请帮助,非常感谢任何提示或想法。
解决方案
100000.00
时间= 30年
低价格= 11%
高利率= 12%
11.00 952.32
11.25 971.26
11.50 990.29
11.75 1009.41
12.00 1028.61
公式:
确定付款的公式为:
(p * k * c)/(c - 1)
p =本金,借入金额
k =月利率(年利率/12.0)
n =每月付款次数(年* 12)
c =(1 + k)^ n
a =每月付款(支付利息和本金)
我已经尝试了我能想到的一切,不知道是什么导致这个逻辑问题!请提供帮助,非常感谢任何提示或想法。
MAIN:
package loantable;
import java.util.ArrayList;
public class LoanTable
{
private double loanAmount;
private double loanLength;
private double lowInterest;
private double highInterest;
private int count;
ArrayList<Double> payments = new ArrayList<>();
public LoanTable(double loanAmount, double loanLength, double lowInterest, double highInterest)
{
this.loanAmount = loanAmount;
this.loanLength = loanLength;
this.lowInterest = lowInterest;
this.highInterest = highInterest;
}
public ArrayList getInterestRates()
{
count = -1;
while(lowInterest <= highInterest)
{
double monthlyInterest = lowInterest / 12.0;
double length = loanLength * 12;
double c = Math.pow((1 + monthlyInterest), length);
double monthlyPayment = (loanAmount * monthlyInterest * c)/ (c - 1);
payments.add(monthlyPayment);
lowInterest += .25;
count++;
}
return payments;
}
public String to()
{
String theory = "";
double currentInterest = highInterest;
ArrayList<Double> t = new ArrayList<>();
t = getInterestRates();
while(count >= 0)
{
theory = theory + "\n" + "Interest Rate: " + currentInterest + " Monthly Payment: " + t.get(count);
count--;
currentInterest -= .25;
}
return theory;
}
}
TESTER:
package loantable;
import java.util.Scanner;
public class LoanTableTester
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Principal: ");
double loanAmount = sc.nextDouble();
System.out.print("\nTime: ");
double time = sc.nextDouble();
System.out.println("\nLow Rate: ");
double lowRate = sc.nextDouble();
System.out.println("\nHigh Rate: ");
double highRate = sc.nextDouble();
LoanTable t = new LoanTable(loanAmount, time, lowRate, highRate);
System.out.println(t.to());
}
}
CONSOLE:
Principal: 100000
Time: 30
Low Rate:
11
High Rate:
12
Interest Rate: 12.0 Monthly Payment: 100000.0
Interest Rate: 11.75 Monthly Payment: 97916.66666666666
Interest Rate: 11.5 Monthly Payment: 95833.33333333334
Interest Rate: 11.25 Monthly Payment: 93750.0
Interest Rate: 11.0 Monthly Payment: 91666.66666666664
CORRECT ANSWER:
Principal = $100000.00
Time = 30 years
Low rate = 11%
High rate = 12%
11.00 952.32
11.25 971.26
11.50 990.29
11.75 1009.41
12.00 1028.61
FORMULA:
The formula for determining payments is:
(p * k * c)/ (c - 1)
p = principal, amount borrowed
k = monthly interest rate (annual rate/12.0)
n = number of monthly payments (years * 12)
c = (1 + k)^n
a = monthly payment (interest and principal paid)
I have tried everything I can think of, not sure what is causing this logic issue! Please help, any tips or ideas are greatly appreciated.
解决方案
100000.00 Time = 30 years Low rate = 11% High rate = 12% 11.00 952.32 11.25 971.26 11.50 990.29 11.75 1009.41 12.00 1028.61
FORMULA:
The formula for determining payments is: (p * k * c)/ (c - 1) p = principal, amount borrowed k = monthly interest rate (annual rate/12.0) n = number of monthly payments (years * 12) c = (1 + k)^n a = monthly payment (interest and principal paid)
I have tried everything I can think of, not sure what is causing this logic issue! Please help, any tips or ideas are greatly appreciated.
这篇关于利息计算器中的逻辑错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文