2016-06-15 177 views
-8

我有亞勒一個問題,我使用循環以下的程序,我無法理解如何使它所以它顯示每月各支付,的Java計算每月付款循環

你要編寫一個程序將顯示給定利率和付款次數的貸款付款時間表。

提示用戶貸款金額,年利率,還款年限。首先,您將使用以下公式計算每月付款。然後,您將編寫一個循環,顯示每月付款明細:利息金額,貸款本金以及貸款餘額。

對於1年期貸款利率(12次月付款)$ 10,000的7%,在繳費明細如下所示:

每月支付= 865.27 付款方式:1利息:58.33校長:806.93餘額:9193.07

這是上面的問題,我不能讓它顯示(x)個月的付款金額,並用它顯示其餘的信息。

import java.text.NumberFormat; 
import java.util.Scanner; 

public class MothlyPay { 

    public static double calculateMonthlyPayment(
     int loanAmount, int termInYears, double interestRate) { 

     // Convert interest rate into a decimal 
     // eg. 6.5% = 0.065 

     interestRate /= 100.0; 

     // Monthly interest rate 
     // is the yearly rate divided by 12 

     double monthlyRate = interestRate/12.0; 

     // The length of the term in months 
     // is the number of years times 12 

     int termInMonths = termInYears * 12; 

     // Calculate the monthly payment 
     // Typically this formula is provided so 
     // we won't go into the details 

     // The Math.pow() method is used calculate values raised to a power 

     double monthlyPayment = 
     (loanAmount*monthlyRate)/
      (1-Math.pow(1+monthlyRate, -termInMonths)); 

     return monthlyPayment; 
    } 

    public static void main(String[] args) { 

     // Scanner is a great class for getting 
     // console input from the user 

     Scanner scanner = new Scanner(System.in); 

     // Prompt user for details of loan 

     System.out.print("Enter loan amount: "); 
     int loanAmount = scanner.nextInt(); 

     System.out.print("Enter loan term (in years): "); 
     int termInYears = scanner.nextInt(); 

     System.out.print("Enter interest rate: "); 
     double interestRate = scanner.nextDouble(); 

     // Display details of loan 

     double monthlyPayment = calculateMonthlyPayment(loanAmount, termInYears, interestRate); 
     double totalPayed = 0; 
     int month = 1; 
     double loanAmountRemaining; 
     // NumberFormat is useful for formatting numbers 
     // In our case we'll use it for 
     // formatting currency and percentage values 
     while(totalPayed <= loanAmount){ 
      totalPayed = totalPayed + monthlyPayment; 
      double totalLoanAmount = loanAmount + interestRate; 
      loanAmountRemaining = totalLoanAmount - totalPayed; 
      month ++; 
     } 
     if(monthlyPayment > loanAmount) 
      totalPayed = totalPayed + loanAmountRemaining; 
    } 

     // Display details of the loan 
//HERE IS THE ISSUE BELOW, I CANT GET IT TO DISPLAY THE AMOUNT OF PAYMENTS FOR (x) MONTHS AND FOR IT TO DISPLAY THE REST OF THE INFO WITH IT FOR THE FOLLOWING VARIABLE LISTED IN THE PRINTF STATEMENT. 

     System.out.printf("%9s  %9s  %9s  %9s\n", "monthlypayment", "interestRate", "loanAmount", "loanAmountRemaining"); 
    } 

//當用戶輸入貸款額度,利率,月份時,需要顯示如下圖。

enter image description here

+6

java!== javascript –

回答

1

現在,你不用打印聲明任何特定的方法,這意味着它不會被調用。我建議把它放在你的主要方法中,然後運行一個循環來查找和打印你的值。

+0

此外,他們需要使用另一個打印語句在循環內打印它們的值。 – Ian

+0

將拼寫錯誤的單詞作爲類名(MothlyPay應該變成「MonthlyPay」)並不是很好的做法。 – MattHampel