Java计算器删除第一行

时间:2011-06-27 23:05:25

标签: java

我需要为我的Java类创建一个抵押贷款计算器,而且我一直在讨论如何删除结果的第一行。我需要用Java(没有图形用户界面)编写一个程序,贷款额为20万美元,利率为5.75%,期限为30年。然后我需要显示抵押贷款支付金额,然后列出贷款余额和贷款期限内每笔付款的利息。

如何制作,以便计算从第1个月开始而不是在0个月开始的每月付款?我想删除第一行$0, $0, $200,000.

import java.text.*;     // Import text formatting classes

public class MortgageCalculator {

    public static void main(String arguments[]) {

        //Variables
        double loanAmount = 200000; // Amount borrowed
        int loanTerm = 360; // Total months of term
        double loanInterest = 0.0575;   // Yearly interest in decimal form

        double monthlyRate = (loanInterest / 12);   //calculate monthly rate

        DecimalFormat df = new DecimalFormat("$###,###.00");    //Formatting the results to decimal form

        // Assign calculation result to monthlyPayment
        double monthlyPayment =
            loanAmount *
            (monthlyRate * Math.pow((1 + monthlyRate), loanTerm)) /
            (Math.pow((1 + monthlyRate), loanTerm) - 1);

        //Print Loan Amount, Interest Rate, Loan Term and Monthly Payment
        System.out.println("The loan amount is: " +
                df.format(loanAmount));

        System.out.println("The intrest rate is: " +
                loanInterest * 100 + "%");

        System.out.println("The term of the loan is: " +
                loanTerm / 12 + " years" + "\n");

        System.out.println("Monthly Payment: " +
                df.format(monthlyPayment) + "\n");

        // New variables

        double balance = loanAmount;
        double monthlyInterest = 0;
        double principal = 0;

        // display 20 lines of results at one time

        // provides columns
        System.out.println("\n\n\nPrincipal\tInterest\tBalance");
        System.out.println("Payment\t\tPayment\t\tRemaining");
        System.out.println("--------- \t--------- \t---------");

        // Start Looping

        int i;

        while (balance > 0) {

            for (i = 1; i < 10; i++) {

                // Display interest, principal, and balance
                System.out.println(df.format(principal) +
                           "\t\t" +
                           df.format(monthlyInterest) +
                           "\t\t" + df.format(balance));

                // New calculations
                monthlyInterest = (balance * monthlyRate);
                principal = (monthlyPayment - monthlyInterest);
                balance = (balance - principal);

            }   // end loop i
            //Pauses screen
            try {
                Thread.sleep(1500);
            }
            catch(InterruptedException e) {
            }
        }       // end while statement

        //Stops loop statement
        if (balance <= 0) {
            System.out.println("The loan balance is: $0.00");
        }
    }
}

4 个答案:

答案 0 :(得分:3)

要删除第一行,只需在调用println方法之前进行计算。

例如:

         for (i = 1; i<10; i++) {

             // New calculations
             monthlyInterest = (balance * monthlyRate);
             principal = (monthlyPayment - monthlyInterest);
             balance = (balance - principal);

             // Display interest, principal, and balance
             System.out.println(df.format(principal) + "\t\t" + df.format(monthlyInterest) + "\t\t" + df.format(balance));

         } // end loop i

您还需要相应地调整for循环中的i。我想这就是你问的问题?

答案 1 :(得分:1)

有各种方法。你可以实施一个柜台。您还可以检查付款金额是否为&gt;打印前为0

答案 2 :(得分:1)

我认为问题在于你嵌套循环的方式。我看到你想要对输出进行分块,以便它不会立即全部转储,但你可以轻松地将它们全部放入一个循环中(提示:查看模数运算符'%')。然后在第一次迭代时使用计数器跳过输出,或者在打印前检查付款值。

答案 3 :(得分:1)

我担心有比第一个有趣的输出线更紧迫的问题:

$1,156.04       $11.11      $1,161.58
$1,161.58       $5.57       $.00
$1,167.15       $.00        -$1,167.15
$1,172.74       -$5.59      -$2,339.88
$1,178.36       -$11.21     -$3,518.24
$1,184.00       -$16.86     -$4,702.25
$1,189.68       -$22.53     -$5,891.92
$1,195.38       -$28.23     -$7,087.30
$1,201.11       -$33.96     -$8,288.41
$1,206.86       -$39.72     -$9,495.27
The loan balance is: $0.00

八个付款太多,而且期末余额非常错误 - $0.00实际上银行欠客户近十年。

我可以理解你出于某种合理的原因每十行添加一次睡眠,但它不应影响贷款数据的正确计算。我强烈建议删除i上的内循环,也许每睡十一次或第二十行输出(可能使用%上的int i运算符仍然有用:)。

要考虑的事项,如果你有足够的时间在这个项目到期之前,就要按照职责划分你的计划:有一个函数计算每月付款,一个函数计算本金与利息的比率,另一个例程将数据打印给用户。这听起来更复杂,但是当您需要在三周内修改此程序以提供图形输出时,您将很快理解将显示例程与计算分开的好处例程。 (这是model-view-controller更大理论的一部分;如果你有时间到期,请仔细阅读维基百科的描述,并尝试了解它在该计划中的用处。)