gpt4 book ai didi

Java贷款表计算器

转载 作者:行者123 更新时间:2023-12-02 04:19:07 30 4
gpt4 key购买 nike

我正在尝试编写一个贷款计算器。我在计算每月付款时遇到问题。我正在尝试打印出不同利率从低到高的每月还款额,增量为 0.25%。

我怀疑我的 while 循环有问题。有什么建议吗?

我一直很感谢你的帮助。我正在努力改进我的问题。

import apcslib.Format;
import chn.util.*;

public class loanTable {


public static void main(String[] args) {
//Declaring Variables
double k;
double n;
double c;
double p;
double a;
double low;
double high;

int time;

//Getting User Input
ConsoleIO console = new ConsoleIO();

System.out.println("Amount of the loan: ");
p = console.readDouble();
System.out.println("Principal = $" + p);

System.out.println("The length of loan in years: ");
time = console.readInt();
System.out.println("Time = " + time + " years");

System.out.println("A low interest rate in %: ");
low = console.readDouble() / 100;
System.out.println("Low rate = " + low + "%");

System.out.println("A high interest rate in %: ");
high = console.readDouble() / 100;
System.out.println("High rate = " + high + "%");

//While Loop
while (low <= high) {

k = low / 1200.0;
n = time * 12;
c = Math.pow((1 + k), n);
a = (p * k * c) / (c - 1);

System.out.println("Annual interest rate: " + low * 100);
System.out.println("Monthly Payment: " + Format.left(a, 2, 2));

low += (0.25 / 100);
}
}

}

`

最佳答案

错误在于 k 的计算。应该是

 k = low /12;

这是正确的版本

public class loanTable {

public static void main(String[] args) {
//Declaring Variables
double k;
double n;
double c;
double p;
double a;
double low;
double high;

int time;

System.out.println("Amount of the loan: ");
p = 100000;
System.out.println("Principal = $" + p);

System.out.println("The length of loan in years: ");
time =30 ;
System.out.println("Time = " + time + " years");

System.out.println("A low interest rate in %: ");
low = 0.11;
System.out.println("Low rate = " + low + "%");

System.out.println("A high interest rate in %: ");
high = 0.1125;
System.out.println("High rate = " + high + "%");

//While Loop
while (low <= high) {
//changed calculation of k
k = low /12;
n = time * 12;
c = Math.pow((1 + k), n);
a = (p * k * c) / (c - 1);

System.out.println("Annual interest rate: " + (low * 100));
System.out.println("Monthly Payment: " +a /* Format.left(a, 2, 2)*/);

low += (0.25 / 100);
}
}
}

回答第二个问题(“我得到的输出直到年利率达到 11.75,但我需要增加到 12.00,我该怎么做?”):由于舍入误差,while 循环在 11.75 处停止。查看评论和一些克服它的替代方案:

public class LoanTable {

public static void main(String[] args) {
//Declaring Variables
double k;
double n;
double c;
double p;
double a;
double low;
double high;

int time;

System.out.println("Amount of the loan: ");
p = 100000;
System.out.println("Principal = $" + p);

System.out.println("The length of loan in years: ");
time =30 ;
System.out.println("Time = " + time + " years");

System.out.println("A low interest rate in %: ");
low = 0.11;
System.out.println("Low rate = " + low + "%");

System.out.println("A high interest rate in %: ");
high = 0.12;
System.out.println("High rate = " + high + "%");

System.out.println("---------------- Not working well ------------------");
//While Loop
while (low <= high) {
/*
* changed calculation of k
*/
k = low /12;
n = time * 12;
c = Math.pow((1 + k), n);
a = (p * k * c) / (c - 1);

System.out.println("Annual interest rate: " + (low * 100));
System.out.println("Monthly Payment: " +a /* Format.left(a, 2, 2)*/);

low += (0.25 / 100);

/*
* at the 4th loop low = 0.12000000000000001
* so low > high and while block is terminated.
*/
System.out.println("low = "+ low);

}

System.out.println("---------------- Alternative 1 ------------------");
//While Loop
low = 0.11;
high = 0.12;
int counter = 0;
double interest = low;

while ( interest <= high ) {

/*
* changed calculation of k
*/
k = interest /12;
n = time * 12;
c = Math.pow((1 + k), n);
a = (p * k * c) / (c - 1);

System.out.println("Annual interest rate: " + (interest * 100));
System.out.println("Monthly Payment: " +a /* Format.left(a, 2, 2)*/);

counter++;
/*
* recalculate interest from low, to avoid accumulating
* rounding error
*/
interest = low + ((0.25/100)* counter);
}

System.out.println("---------------- Alternative 2 ------------------");
//While Loop
low = 0.11;
high = 0.12;

/*
* cast float to reduce the accuracy. A better alternative would be
* to declare low and high as float
*/
while ( (float)low <= (float)high ) {
/*
* changed calculation of k
*/
k = low /12;
n = time * 12;
c = Math.pow((1 + k), n);
a = (p * k * c) / (c - 1);

System.out.println("Annual interest rate: " + (low * 100));
System.out.println("Monthly Payment: " +a /* Format.left(a, 2, 2)*/);

low += (0.25 / 100);

}

System.out.println("---------------- Alternative 3 ------------------");

//While Loop
/*
* work with integer
*/
int lowInterest = 1100;
int highInterest = 1200;

while ( lowInterest <= highInterest ) {
/*
* changed calculation of k
*/
k = (float)lowInterest /(12*10000);
n = time * 12;
c = Math.pow((1 + k), n);
a = (p * k * c) / (c - 1);

System.out.println("Annual interest rate: " + ((float)lowInterest /100));
System.out.println("Monthly Payment: " +a /* Format.left(a, 2, 2)*/);

lowInterest += 25;

}

}
}

关于Java贷款表计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32983609/

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