Java计算器删除第一行

Java计算器删除第一行,java,Java,我需要为我的Java类创建一个抵押计算器,我整天都在绞尽脑汁想如何删除第一行结果。我需要用Java编写一个程序(没有图形用户界面),使用20万美元的贷款,利率为5.75%,期限为30年。然后我需要显示抵押付款金额,然后列出贷款余额和贷款期限内每次付款支付的利息 如何使其计算从第1个月开始而不是从第0个月开始的每月付款?我想删除第一行$0,$0,$200000。 import java.text.*; // Import text formatting classes public cl

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

如何使其计算从第1个月开始而不是从第0个月开始的每月付款?我想删除第一行
$0,$0,$200000。

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");
        }
    }
}
导入java.text.*;//导入文本格式类
公共类抵押计算器{
公共静态void main(字符串参数[]){
//变数
double loanAmount=200000;//借款金额
int loanTerm=360;//学期总月数
双倍贷款利息=0.0575;//十进制形式的年利息
双月利率=(贷款利率/12);//计算月利率
DecimalFormat df=新的DecimalFormat($#######,####.00”);//将结果格式化为十进制格式
//将计算结果指定给月付款
双月付款=
洛纳蒙特*
(月龄*数学功率((1+月龄),loanTerm))/
(数学能力((1+月),loanTerm)-1;
//打印贷款金额、利率、贷款期限和每月付款
System.out.println(“贷款金额为:+
格式(loanAmount));
System.out.println(“输入速率为:”+
贷款利率*100+“%”;
System.out.println(“贷款期限为:+
loanTerm/12+“年”+“\n”);
System.out.println(“每月付款:+
df.格式(月付款)+“\n”);
//新变量
双倍余额=贷款金额;
双月利息=0;
双主体=0;
//一次显示20行结果
//提供列
System.out.println(“\n\n\nPrincipal\tInterest\tBalance”);
System.out.println(“付款\t\t付款\t\t付款”);
System.out.println(“-----------\t-----------\t------------”);
//开始循环
int i;
而(余额>0){
对于(i=1;i<10;i++){
//显示利息、本金和余额
System.out.println(df.format(principal)+
“\t\t”+
df.格式(monthlyInterest)+
“\t\t”+df.format(balance));
//新计算
月利息=(余额*月利率);
本金=(月付款-月利息);
余额=(余额-本金);
}//结束循环i
//暂停屏幕
试一试{
睡眠(1500);
}
捕捉(中断异常e){
}
}//结束while语句
//停止循环语句

如果(余额有多种方法。您可以实现计数器。您还可以在打印前检查已付款金额是否>0有多种方法。您可以实现计数器。您还可以在打印前检查已付款金额是否>0要删除第一行,只需在
打印前进行计算即可调用tln
方法

例如:

         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;i要删除第一行,只需在调用
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;i我认为问题在于嵌套循环的方式。我发现您希望将输出分块,以便它不会一次全部转储,但您可以轻松地将所有内容放入一个循环中(提示:查看模运算符“%”)。然后在第一次迭代时使用计数器跳过输出,或者在打印之前检查付款值。

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

恐怕比第一行更紧迫的问题是:

$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美元
,而实际上银行欠客户近一万美元

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


如果你有足够的时间在这个项目到期之前,将你的程序划分为职责划分:有一个函数计算月支付,一个函数计算主息比,另一个程序将数据打印给用户。这样听起来更复杂,但是当你需要的时候。要在三周内修改此程序以提供图形输出,您将很快了解将显示例程与计算例程分开的好处。(这是一个更大的理论的一部分;如果您在到期之前有时间,请通读维基百科的描述,并尝试了解它在本程序中的作用。)

恐怕还有比第一行有趣的输出更紧迫的问题:

$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美元
,而实际上银行欠客户的