【问题标题】:Java Computing Monthly Payments LoopsJava 计算每月支付循环
【发布时间】:2016-10-17 02:05:45
【问题描述】:

我有一个问题要问,我正在为以下程序使用循环,但我不明白如何制作它,所以它会显示每月的每笔付款,

您将编写一个程序,该程序将在给定利率和付款次数的情况下显示贷款的付款时间表。

提示用户贷款金额、年利率、还款年限。首先,您将使用以下公式计算每月付款。然后,您将编写一个循环显示每月付款明细:利息金额、应用于贷款的本金和贷款余额。

对于 7% 的 10,000 美元的 1 年期贷款(12 个月付款),付款明细如下所示:

每月付款 = 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");
   }

//当用户输入贷款金额、利率、月份时,需要如下图所示。

【问题讨论】:

  • java !== javascript

标签: java loops methods


【解决方案1】:

现在,您没有任何特定方法的 print 语句,这意味着它不会被调用。我建议将它放在你的 main 方法中,然后运行一个循环来查找和打印你的值。

【讨论】:

  • 他们还需要使用另一个打印语句在循环中打印它们的值。
  • 并且将拼写错误的单词作为类名并不是很好的做法(MothlyPay 应该变成“MonthlyPay”)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-11
  • 1970-01-01
相关资源
最近更新 更多