【发布时间】: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