Java可贷计算器

Java可贷计算器,java,Java,我正试图编写一个贷款计算器。我在计算每月付款时遇到问题。我试图打印出从低到高的不同利率的每月付款,增加0.25% 我怀疑我的while循环有问题。有什么建议吗 我一直很感激你的帮助。我正在努力改进我的问题 import apcslib.Format; import chn.util.*; public class loanTable { public static void main(String[] args) { //Declaring Variables

我正试图编写一个贷款计算器。我在计算每月付款时遇到问题。我试图打印出从低到高的不同利率的每月付款,增加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);
        }
    }

}
导入apcslib.Format;
导入chn.util.*;
公共类贷款{
公共静态void main(字符串[]args){
//声明变量
双k;
双n;
双c;
双p;
双a;
双低;
双高;
整数时间;
//获取用户输入
ConsoleIO console=新ConsoleIO();
System.out.println(“贷款金额:”);
p=console.readDouble();
System.out.println(“本金=$”+p);
System.out.println(“贷款年数:”);
time=console.readInt();
System.out.println(“Time=“+Time+”年”);
System.out.println(“低利率%:”;
low=console.readDouble()/100;
System.out.println(“低比率=”+低+“%”);
System.out.println(“高利率%:”);
high=console.readDouble()/100;
System.out.println(“高比率=”+高+“%”);
//While循环

而(低假设您正在使用:

  • I:年息:11%
  • P:贷款现值100000美元
  • N:期限:30年
计算每月还款的正常公式为:

(P*I/12)/(1-(1+I/12)^(-n*12))
或者,在Java中:

private double monthlyRepayment(double loanValue, double annualInterest, int termInYears) {
    double monthlyInterest = annualInterest / 12;
    double termInMonths = termInYears * 12;
    return loanValue * monthlyInterest / (1 - Math.pow(1 + monthlyInterest, -termInMonths));
}

这正确地将还款计算为$952.32

错误在于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);
    }
  }
}
公共类可贷{
公共静态void main(字符串[]args){
//声明变量
双k;
双n;
双c;
双p;
双a;
双低;
双高;
整数时间;
System.out.println(“贷款金额:”);
p=100000;
System.out.println(“本金=$”+p);
System.out.println(“贷款年数:”);
时间=30;
System.out.println(“Time=“+Time+”年”);
System.out.println(“低利率%:”;
低=0.11;
System.out.println(“低比率=”+低+“%”);
System.out.println(“高利率%:”);
高=0.1125;
System.out.println(“高比率=”+高+“%”);
//While循环

虽然(低)您表示您在输出方面遇到了问题,但没有告诉我们是什么问题。请通过具体和充分的描述帮助我们。我的输出是,年利率:11.0月付款:282.40年利率:11.25月付款:282.50年利率:11.5月付款:282.61年利率:11.75月付款:282.72他们应该是,11.00 952.32 11.25 971.26 11.50 990.29 11.75 1009.41 12.00 1028.61你能解释一下
格式吗。left
call?据我所知
格式
是一个抽象类没有
left
method.Format.left将我的输出设置为小数点后2位。哇,谢谢你。最后一个问题是,我的输出直到年利率达到11.75,但我需要增加到12.00,我该怎么做?@MichaelGuest:现在做一个好公民:如果这个答案对你有帮助,你应该记住o投票吧,如果它回答了你的问题,那就投票吧。谢谢这个公式帮了我大忙!@MichaelGuest:现在做一个好公民:如果这个答案帮了你,你应该记得投票吧,如果它回答了你的问题,那就投票吧。
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;

         }

      }
    }