lundi 2 mars 2015

Java: How do I display how many months it took?

Heres my code:



import java.util.*;

public class LoanCalculator {

static Scanner console = new Scanner(System.in);

public static void main(String[] args) {

//Declare variables
double loanAmount;
double interestRate;
double monthlyPayment;
double interest = 0;
double principal = 0;
int months;

System.out.print("Enter the amount of your loan: ");
loanAmount = console.nextDouble();
System.out.println();

System.out.print("Enter the interest rate per year: ");
interestRate = console.nextDouble();
System.out.println();

System.out.print("Enter your monthly payment amount: ");
monthlyPayment = console.nextDouble();
System.out.println();

interestRate = (interestRate / 12) /100;

for (months = 1; months <= loanAmount; months++)
{
interest = (loanAmount * interestRate);
principal = monthlyPayment - interest;
loanAmount = loanAmount - principal;
System.out.println(months);
}
}
}


I want the program to output how many months the loan took to pay off, but it's listing every month. How can I do this? Also if the monthly payment is less then the first month's interest then the loan amount will increase, how can I warn the user if this happens?


Aucun commentaire:

Enregistrer un commentaire