【问题标题】:While loop in monthly amortization calculator runs after 0 [Java]每月摊销计算器中的循环在 0 之后运行 [Java]
【发布时间】:2016-10-10 19:18:22
【问题描述】:

我几乎完成了一个基于用户本金、每月利息和每月还款额的每月摊还计算器的编程。但是最后一个问题我似乎无法弄清楚。我似乎已将限制设置为 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();
     }
  }

【问题讨论】:

  • 它按预期工作ideone.com/d7ONH4,您正在尝试什么输入?
  • 您的本金是多少? 700000 你的月利率是多少? .005 您的月供是多少? 4196.85
  • 预期的输出是什么?
  • 这是最后两次运行的结果:“第 29 年,第 12 个月:您的利息金额为 20.9 美元您的本金金额为 4176.0 美元您的新余额为 3.45 美元第 30 年,第 1 个月:您的利息金额为$0.02 您的本金金额 $4197.0 您的新余额是 $-4193.38" 但我不希望它显示“第 30 年,第 1 个月:您的利息金额是 $0.02 您的本金金额 $4197.0 您的新余额是 $-4193.38”

标签: java while-loop java.util.scanner public amortization


【解决方案1】:

问题是当循环在第 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++;
  }

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

【讨论】:

    【解决方案2】:

    您的代码几乎没有问题。

    • 每个月的利息和本金之和不等于每月还款。
    • 不需要您计算的“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-17
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 2018-03-11
      • 2023-03-17
      • 1970-01-01
      相关资源
      最近更新 更多