我需要为我的Java类创建一个抵押计算器,并且整天都在思考如何删除结果的第一行。我需要用Java(没有图形用户界面)编写一个程序,使用200,000美元的贷款,利率为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");
}
}
}
最佳答案
要删除第一行,只需在调用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
。我想这就是你要问的吗?关于java - Java计算器删除第一行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6499957/