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

あなたのコードにはいくつかの問題があります。

  • 毎月の利息と元本の支払いの合計は、毎月の支払いと等しくありません。
  • あなたが計算した「実際の支払い」は必要ありません。
  • 支払いを整数に丸める必要はありません。
  • ユーザーから毎月の支払いを受け取っているため、ユーザーは先月の毎月の支払いよりも少なく支払う必要があり、それが -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 に答える
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ます。

解決策の 1 つは、バランスを計算した後、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 に答える