ユーザーの元金、毎月の利息、毎月の支払いに基づく毎月の償却計算機のプログラミングはほぼ完了しています。しかし、私が理解できないように見える最後の問題があります。制限を 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();
}
}