我写的程序滚动直到它结束。我需要它只滚动12个月停止然后按下进入继续到接下来的12个月。在此期间,我需要每次都保持标题。请提供帮助。
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Assign3_paulpoland;
/**
*
* @author PAUL
*/
public class Assign3_PaulPoland
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
calculateMonthlyPayment();
}
public static void calculateMonthlyPayment()
{
//P = principal, the initial amount of the loan
double dblP = 200000.00;
//I = the annual interest rate (from 1 to 100 percent)
double dblI = 5.75;
//L = length, the length (in years) of the loan, or at least the length
over which the loan is amortized.
int intL = 30;
//The following assumes a typical conventional loan where the interest is
compounded monthly. First I will define two more variables to make the calculations
easier:
//J = monthly interest in decimal form = I / (12 x 100)
double dblJ = dblI / (12 * 100);
//N = number of months over which loan is amortized = L x 12
int intN = intL * 12;
//
//Okay now for the big monthly payment (M) formula, it is:
// M = P * (J/ (1 - (1 + J)^ -N)
// J
// M = P x ------------------------
//
// 1 - ( 1 + J ) ^ -N
//
//where 1 is the number one (it does not appear too clearly on some
browsers)
double dblM = dblP * (dblJ / (1 - Math.pow((1 + dblJ), - intN)));
System.out.printf("\n");
//Print to screen principal
printHeader(dblP, dblI, intL, dblM);
// So to calculate it, you would first calculate 1 + J then take that to
the -N (minus N) power, subtract that from the number 1. Now take the inverse of that
(if you have a 1/X button on your calculator push that). Then multiply the result
times J and then times P. Sorry, for the long way of explaining it, but I just wanted
to be clear for everybody.
//
// The one-liner for a program would be (adjust for your favorite
language):
//
// M = P * ( J / (1 - (1 + J) ** -N))
calculateAmortizationTable(dblP,dblJ,dblM);
}
public static void calculateAmortizationTable(double dblP,double dblJ, double dblM)
{
// So now you should be able to calculate the monthly payment, M.
// To calculate the amortization table you need to do some iteration (i.e.
a simple loop).
// I will tell you the simple steps :
int intCounter=0;
while(dblP > +1){
intCounter++;
// Step 1: Calculate H = P x J, this is your current monthly interest
double dblH = dblP*dblJ;
// Step 2: Calculate C = M - H, this is your monthly payment minus your
monthly interest, so it is the amount of principal you pay for that month
double dblC = dblM-dblH;
// Step 3: Calculate Q = P - C, this is the new balance of your principal
of your loan.
double dblQ = dblP-dblC;
// Step 4: Set P equal to Q and go back to Step 1: You thusly loop around
until the value Q (and hence P)
// goes to zero.
dblP=dblQ;
System.out.print(intCounter);
System.out.printf("\t$%.2f",dblP);
System.out.printf("\t$%.2f",dblH);
System.out.printf("\t\t$%.2f",dblC);
System.out.print("\n");
if ((intCounter % 12) == 0);
// {
// pageBreak();
// printTableHeader();
// }
// System.out.print(" Payment # " + intCounter);
// System.out.printf(" Loan Balance = $%.2f",dblP);
// System.out.printf(" Interest Paid = %.2f",dblH);
// System.out.printf(" Principal Paid = $%.2f",dblC);
// System.out.print("\n");
}
}
public static void printHeader(double dblP, double dblI, int intY, double dblM)
{
System.out.println("Welcome to McBride Financial Mortgage Calculator\n\n");
System.out.printf("Principal \t= $%.2f",dblP);
System.out.println(" ");
System.out.println("Interest \t= " + dblI + "%");
System.out.println("Years \t= " + intY);
System.out.println(" ");
System.out.printf("Monthly Payment \t= $%.2f\n\n",dblM);
System.out.println("Month Loan Balance Principal Interest");
System.out.println("------------------------------------------------");
修改
清理版的类:
public class Assign3_PaulPoland {
public static void main(String[] args) {
calculateMonthlyPayment();
}
public static void calculateMonthlyPayment() {
double dblP = 200000.00;
double dblI = 5.75;
int intL = 30;
double dblJ = dblI / (12 * 100);
int intN = intL * 12;
double dblM = dblP * (dblJ / (1 - Math.pow((1 + dblJ), -intN)));
System.out.printf("\n");
printHeader(dblP, dblI, intL, dblM);
calculateAmortizationTable(dblP, dblJ, dblM);
}
public static void calculateAmortizationTable(double dblP, double dblJ,
double dblM) {
int intCounter = 0;
while (dblP > +1) {
intCounter++;
double dblH = dblP * dblJ;
double dblC = dblM - dblH;
double dblQ = dblP - dblC;
dblP = dblQ;
System.out.print(intCounter);
System.out.printf("\t$%.2f", dblP);
System.out.printf("\t$%.2f", dblH);
System.out.printf("\t\t$%.2f", dblC);
System.out.print("\n");
if ((intCounter % 12) == 0) {
// TODO: code goes here
}
}
}
public static void printHeader(double dblP, double dblI, int intY,
double dblM) {
System.out.println("Welcome to McBride Financial Mortgage Calculator\n\n");
System.out.printf("Principal \t= $%.2f", dblP);
System.out.println(" ");
System.out.println("Interest \t= " + dblI + "%");
System.out.println("Years \t= " + intY);
System.out.println(" ");
System.out.printf("Monthly Payment \t= $%.2f\n\n", dblM);
System.out.println("Month Loan Balance Principal Interest");
System.out.println("------------------------------------------------");
}
}