开发者

Java Calculator Remove first line

开发者 https://www.devze.com 2023-03-15 23:48 出处:网络
I need to create a Mortgage Calculator for my Java class, and I have been racking my brain all DAY about how to remove the first line of the results. I need to write a program in Java (without a graph

I need to create a Mortgage Calculator for my Java class, and I have been racking my brain all DAY about how to remove the first line of the results. I need to write a program in Java (without a graphical user interface) using a loan amount of $200,000 with an interest rate of 5.75% and a 30 year term. Then I need to display the mortgage payment amount and then list the loan balance and interest paid for each payment over the term of the loan.

How do I make it so it calculates the monthly payments starting at month 1 and NOT at month 0? I want to remove the first line $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" +
    开发者_StackOverflow                       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");
        }
    }
}


To remove the first line just do the calculations before the println method is called.

For example:

         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

You also need to adjust i in the for-loop accordingly. I think this is what you were asking?


There are various approaches. You coould implement a counter. Your could also check if the amount paid off > 0 before printing


I think the problem lies in the way you're nesting loops. I see that you want to chunk up the output so that it doesn't all dump out at once, but you can easily bring that all into one loop (hint: look at the modulus operator '%'). Then either skip output on the first iteration, using a counter, or check the payment value before printing.


I'm afraid there are more pressing problems than the first funny output line:

$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

There are eight payments too many, and the ending balance is very wrong -- $0.00 when in reality the bank owes the customer nearly ten grand.

I can appreciate that you added the sleep every ten lines for some reasonable reason, but it shouldn't effect the correct calculation of the loan data. I strongly recommend removing the inner loop on i and perhaps sleeping every tenth or twentieth line of output (perhaps using the % operator on that int i that can still be useful :).

Something to consider, if you have enough time before this project is due, is splitting apart your program along a division of duties: Have a function calculate the monthly payment, one function calculate the ratio of principal vs interest, and another routine to print the data to the user. It sounds more complicated this way, but when you need to amend this program in three weeks to provide a graphical output, you'll quickly understand the benefits of keeping display routines separate from computation routines. (This is part of a larger theory of model-view-controller; if you've got time before this is due, please read through the Wikipedia description and try to understand where it would be useful in this program.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号