首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java计算器删除第一行

问Java计算器删除第一行
EN

Stack Overflow用户
提问于 2011-06-28 07:05:25
回答 4查看 1.5K关注 0票数 1

我需要为我的Java类创建一个Mortgage Calculator,并且我整天都在绞尽脑汁地思考如何删除结果的第一行。我需要用Java编写一个程序(没有图形用户界面),贷款金额为200,000美元,利率为5.75%,期限为30年。然后,我需要显示抵押贷款付款金额,然后列出贷款余额和在贷款期限内为每笔付款支付的利息。

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

代码语言:javascript
复制
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");
        }
    }
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-06-28 07:13:15

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

例如:

代码语言:javascript
复制
         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。我想这就是你要问的?

票数 3
EN

Stack Overflow用户

发布于 2011-06-28 07:12:42

有不同的方法。你可以实现一个计数器。您还可以在打印前检查支付的金额是否>0

票数 1
EN

Stack Overflow用户

发布于 2011-06-28 07:26:01

我认为问题出在你嵌套循环的方式上。我看到你想把输出分成块,这样它就不会一次全部转储出来,但你可以很容易地把所有这些都放到一个循环中(提示:看看模运算符'%')。然后使用计数器跳过第一次迭代的输出,或者在打印之前检查支付值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6499957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档