0

我几乎完成了一个基于用户本金、每月利息和每月付款的每月摊销计算器的编程。但是最后一个问题我似乎无法弄清楚。我似乎已将限制设置为 0,但如果是负资金,它仍会显示第一个金额。这是更好地理解我的意思的代码:

import java.util.Scanner;

public class Amortization {

   public static void main(String []args){

       Scanner input = new Scanner(System.in);
       int month = 1;
       int year = 0;

       double balance;
       double rate;
       double payment;
       double principal;
       double calculated_interest;
       double actual_payment;
       double principal_amt;

       System.out.println("What is your principal amount?"); principal = input.nextDouble(); balance = principal;
       System.out.println("What is your monthly interest rate in decimal?"); rate = input.nextDouble();
       System.out.println("What is your monthly payment?"); payment = input.nextDouble();


       while(balance>0){

          if(month == 13){
             year++;
             month = 1;
          }

          calculated_interest = ((int)(Math.round(balance*rate*100)))/100.0;
          principal_amt = ((int)(Math.round((payment-calculated_interest))*100))/100.0;
          actual_payment = ((int)(Math.round((payment-calculated_interest)*100)))/100.0;

          System.out.println("Year " + year + ", " + "Month " + month + ":");
          System.out.println("Your interest amount is " + "$" + calculated_interest);
          System.out.println("Your principal amount " + "$" + principal_amt);
          balance = ((int)(Math.round((balance-actual_payment)*100)))/100.0;

          System.out.println("Your new balance is " + "$" + balance);
          System.out.println();

          month++;
      }
      input.close();
     }
  }
4

2 回答 2

0

问题是当循环在第 29 年第 12 个月时,值是

Year 29, Month 12:
Your interest amount is $20.9
Your principal amount $4176.0
Your new balance is $3.45

现在,此时 的值balance不小于0,因此while为真,当计算新值时,余额 ( balance = ((int)(Math.round((balance-actual_payment)*100)))/100.0;) 变为负数,为balance - atual_payment will return negative because the previous balance is $3.45

解决方案之一是在计算余额后在 while 循环中添加另一个 if 条件。

while(balance>0){

      calculated_interest = ((int)(Math.round(balance*rate*100)))/100.0;
      principal_amt = ((int)(Math.round((payment-calculated_interest))*100))/100.0;
      actual_payment = ((int)(Math.round((payment-calculated_interest)*100)))/100.0;
      balance = ((int)(Math.round((balance-actual_payment)*100)))/100.0;

      if (balance < 0){
        break; //this will break out of the loop
      }

      if(month == 13){
         year++;
         month = 1;
      }
      System.out.println("Year " + year + ", " + "Month " + month + ":");
      System.out.println("Your interest amount is " + "$" + calculated_interest);
      System.out.println("Your principal amount " + "$" + principal_amt);

      System.out.println("Your new balance is " + "$" + balance);
      System.out.println();

      month++;
  }

此代码不会打印出负余额。

于 2016-10-10T19:49:50.443 回答
0

您的代码几乎没有问题。

  • 每个月的利息和本金之和不等于每月还款。
  • 不需要您计算的“actual_payment”。
  • 您无需将付款四舍五入为整数。
  • 因为您从用户那里收取每月付款,所以用户在上个月支付的每月付款将少于每月付款,这就是导致 -ve 如果您希望每个月的付款相等,您必须自己计算每月付款。

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int month = 1;
        int year = 0;
    
        double balance;
        double rate;
        double payment;
        double principal;
        double calculated_interest;
        double actual_payment;
        double principal_amt;
    
        System.out.println("What is your principal amount?");
        principal = input.nextDouble();
        balance = principal;
        System.out.println("What is your monthly interest rate in decimal?");
        rate = input.nextDouble();
        System.out.println("What is your monthly payment?");
        payment = input.nextDouble();
    
        while (balance > 0) {
    
            if (month == 13) {
                year++;
                month = 1;
            }
            calculated_interest = (balance * rate * 100) / 100.0;
            principal_amt = payment - calculated_interest;
    
            System.out.println("Year " + year + ", " + "Month " + month + ":");
            System.out.println("Your interest amount is " + "$" + calculated_interest);
    
            if (balance > payment) {
    
                balance = ((int) (Math.round((balance - principal_amt) * 100))) / 100.0;
                System.out.println("Your principal amount " + "$" + principal_amt);
                System.out.println("Your new balance is " + "$" + balance);
            } else {
                System.out.println("Your principal amount " + "$" + (balance - calculated_interest));
                System.out.println("Your new balance is " + "$" + 0);
                System.out.println("You'll only pay $" + (calculated_interest + balance) + " this month.");
                break;
            }
    
            System.out.println();
    
            month++;
        }
        input.close();
    }
    
于 2016-10-10T19:54:46.123 回答