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