Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 贷款计算器程序的数学问题_Java - Fatal编程技术网

Java 贷款计算器程序的数学问题

Java 贷款计算器程序的数学问题,java,Java,这是一个贷款计算器程序。我的数学有问题。除2个月后的期初余额外,其他一切似乎都是正确的。请注意,第3个月的期初余额与第2个月的期末余额不同。接下来的几个月也是如此。我一直想把它修好,但没有成功。我需要他们是相同的,所以最后一个月的期末余额将是0 这是程序的示例输出: 以下是节目: 公共类贷款计算器{ 公共静态void main(字符串[]args){ System.out.println(“个人贷款支付计算器”);//打印程序名称 System.out.println(“============

这是一个贷款计算器程序。我的数学有问题。除2个月后的期初余额外,其他一切似乎都是正确的。请注意,第3个月的期初余额与第2个月的期末余额不同。接下来的几个月也是如此。我一直想把它修好,但没有成功。我需要他们是相同的,所以最后一个月的期末余额将是0

这是程序的示例输出: 以下是节目:
公共类贷款计算器{
公共静态void main(字符串[]args){
System.out.println(“个人贷款支付计算器”);//打印程序名称
System.out.println(“=======================================================”);
扫描仪键盘=新扫描仪(System.in);//定义连接到键盘的扫描仪对象
String badInput;//将非整数或非双精度输入分配给badInput
System.out.print(“输入贷款金额:”;//提示用户输入贷款金额
而(!keyboard.hasNextDouble())//第一个输入值是双精度的吗?
{
badInput=键盘。下一步();
System.out.println(“错误:应为双精度,遇到:”+badInput);
System.out.println(“请以双精度输入贷款金额:”);
}
double loanAmount=keyboard.nextDouble();//将第一个输入分配给loanAmount
System.out.print(“输入贷款期限(月):”;//提示用户输入月数
而(!keyboard.hasNextInt())//第二个输入值是int吗?
{
badInput=键盘。下一步();
System.out.println(“错误:应为整数,遇到:“+badInput”);
System.out.println(“请输入整数形式的贷款期限:”);
}
int loanTerm=keyboard.nextInt();//将第二个输入分配给loanTerm
System.out.print(“输入利率%(每年):”;//提示用户输入利率
而(!keyboard.hasNextDouble())//第一个输入值是双精度的吗?
{
badInput=键盘。下一步();
System.out.println(“错误:应为整数,遇到:“+badInput”);
System.out.println(“请以双精度输入贷款金额:”);
}
double interestRate=keyboard.nextDouble();//将第三个输入分配给interestRate
System.out.println();//跳过一行
System.out.println(“贷款支付和摊销表”);
System.out.printf(“%s”,”======================================================================================================================”);
System.out.println();
系统输出打印(“5s%10s%10s%10s%10s%10s%10s%10s”、“月份”、“开始”、“周一”、“本金”、“利息”、“结束”);
System.out.println();
System.out.printf(“%5s%10s%10s%10s%10s%10s%10s”、“#”、“余额”、“付款”、“已付款”、“已付款”、“余额”);
System.out.println();
System.out.printf(“%s”,”======================================================================================================================”);
System.out.println();
双月桂酸酯=(利率/100.0)/12.0;
双月付款=(月利率*贷款金额)/(1-(数学功率(1+月利率,-贷款金额));
双起点余额=贷款金额;
双利期=期初余额*月利率;
双倍本金援助=月付款-利息援助;
支付的利息总额=0;
for(int-monthCount=0;monthCount
错误非常简单:

beginningBalance = loanAmount - principalPaid * monthCount;
记住,“principalPaid”每月都在增加。支付的本金总额不是最后一次principalPaid*总人数,而是所有月份支付的本金总额

你可以像支付利息一样为principalPaid创建一个连续的总额。 但是做开始平衡=上个月结束平衡要容易得多

 public class LoanCalculator {

    public static void main(String[] args) {
            System.out.println("Personal Loan Payment Calculator"); // print the name of the program
            System.out.println("================================");
            Scanner keyboard = new Scanner (System.in); // define a Scanner object attached to a keyboard
            String badInput; // assign non-integer or non-double inputs to badInput

            System.out.print("Enter a loan amount: "); // prompt the user to enter loan amount
            while ( ! keyboard.hasNextDouble()) // is the first input value a double?
            {
                badInput = keyboard.next();
                System.out.println("Error: expected a Double, encountered: " + badInput);
                System.out.println("Please enter a loan amount in Double: ");
            }
            double loanAmount = keyboard.nextDouble(); // assign the first input to loanAmount

            System.out.print("Enter the loan term (months): "); // prompt the user to enter number of months
            while ( ! keyboard.hasNextInt()) // is the second input value an int?
            {
                badInput = keyboard.next();
                System.out.println("Error: expected an Integer, encountered: " + badInput);
                System.out.println("Please enter a loan term in Integer: ");
            }
            int loanTerm = keyboard.nextInt(); // assign the second input to loanTerm

            System.out.print("Enter the interest rate (% per year): "); // prompt the user to enter the interest rate
            while ( ! keyboard.hasNextDouble()) // is the first input value a double?
            {
                badInput = keyboard.next();
                System.out.println("Error: expected an integer, encountered: " + badInput);
                System.out.println("Please enter a loan amount in Double: ");
            }
            double interestRate = keyboard.nextDouble(); // assign the third input to interestRate

            System.out.println(); // skip a line

            System.out.println("             Loan Payment and Amortization Table");
            System.out.printf("%s", "=============================================================");
            System.out.println();
            System.out.printf("%5s %10s %10s %10s %10s %10s", "Months" ,"Beginning", "Monhtly", "Principal", "Interest", "Ending");
            System.out.println();
            System.out.printf(" %5s %10s %10s %10s %10s %10s", "#","Balance", "Payment", "Paid", "Paid", "Balance");
            System.out.println();
            System.out.printf("%s ", "=============================================================");
            System.out.println();

            double monthlyRate = (interestRate / 100.0) / 12.0;
            double monthlyPayment = (monthlyRate * loanAmount) / ( 1 - (Math.pow( 1 + monthlyRate, - loanTerm)));
            double beginningBalance = loanAmount;
            double interestPaid = beginningBalance * monthlyRate;
            double principalPaid = monthlyPayment - interestPaid;

            int total_interest_paid = 0;
            for (int monthCount = 0 ; monthCount < loanTerm ; ++monthCount)
            {
              int months = 1 + monthCount;
              beginningBalance = loanAmount - principalPaid * monthCount; 
              interestPaid = beginningBalance * monthlyRate;
              principalPaid = monthlyPayment - interestPaid;            
              double endingBalance = beginningBalance - principalPaid;
              System.out.printf(" %5d %10.2f %10.2f %10.2f %10.2f %10.2f\n", months, beginningBalance, monthlyPayment, principalPaid, interestPaid, endingBalance);

              total_interest_paid += interestPaid;
            }


            System.out.printf("%s ", "=============================================================");
            System.out.println();

            NumberFormat currency = NumberFormat.getCurrencyInstance();
            DecimalFormat percentFormat = new DecimalFormat ("0.00");

            System.out.println("\nSummary:");
            System.out.println("========");
            System.out.println("Loan Amount:           " + currency.format(loanAmount));
            System.out.println("Monthly Payment:       " + currency.format(monthlyPayment));
            System.out.println("Number of Payments:    " + loanTerm);
            System.out.println("Total Interest Paid:   " + currency.format(total_interest_paid));
            System.out.println("Annual Interest Rate:  " + percentFormat.format(interestRate) + "%");       
        }
}
beginningBalance = loanAmount - principalPaid * monthCount;