如何解决车辆贷款计算器-我的程序无法输出正确的值
我一直在尝试使摊销计算器正常工作,但是期末付款余额并未以0结尾,并且我的代码未输出正确的值,并且在进行了几个小时的谷歌搜索后,我陷入了困境。我认为我的问题在评论“列表框循环”的下面。任何帮助将不胜感激。
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender,EventArgs e)
{
// Allows for the hotkeys to be used even when out of focus from main form
this.KeyPreview = true;
}
private void MainForm_KeyPress(object sender,KeyPressEventArgs e)
{
// Adds hotkeys; Enter = Calculate,Escape = Exit
if (e.KeyChar == (char)Keys.Enter)
{
calculateButton.PerformClick();
}
else if (e.KeyChar == (char)Keys.Escape)
{
exitButton.PerformClick();
}
}
private void rebateCheck_CheckedChanged(object sender,EventArgs e)
{
// Enables & Disables rebate textbox based on rebate checkbox
if (rebateCheck.Checked == true)
{
rebateBox.Enabled = true;
}
else
{
rebateBox.Clear();
rebateBox.Enabled = false;
}
}
/* Selects data inside of the textbox when tabbing or clicking into it */
private void loanAmountBox_Enter(object sender,EventArgs e)
{
loanAmountBox.SelectAll();
}
private void loanAmountBox_Click(object sender,EventArgs e)
{
loanAmountBox.SelectAll();
}
private void annualAPRBox_Enter(object sender,EventArgs e)
{
annualAPRBox.SelectAll();
}
private void annualAPRBox_Click(object sender,EventArgs e)
{
annualAPRBox.SelectAll();
}
private void rebateBox_Enter(object sender,EventArgs e)
{
rebateBox.SelectAll();
}
private void rebateBox_Click(object sender,EventArgs e)
{
rebateBox.SelectAll();
}
/* Clears the list box when text is changed on any of the input boxes */
private void loanAmountBox_TextChanged(object sender,EventArgs e)
{
loanListBox.Items.Clear();
}
private void annualAPRBox_TextChanged(object sender,EventArgs e)
{
loanListBox.Items.Clear();
}
private void rebateBox_TextChanged(object sender,EventArgs e)
{
loanListBox.Items.Clear();
}
/* Only allows digits,periods,and control keys to be entered into textboxes */
private void loanAmountBox_KeyPress(object sender,KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
return;
}
}
private void annualAPRBox_KeyPress(object sender,KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
return;
}
}
private void rebateBox_KeyPress(object sender,KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
return;
}
}
private void exitButton_Click(object sender,EventArgs e)
{
// Asks the user if they are sure they want to exit
DialogResult dialog = MessageBox.Show("Are you sure you want to exit?",this.Text,MessageBoxButtons.YesNo,MessageBoxIcon.Warning); ;
if (dialog == DialogResult.Yes)
this.Close();
}
private void calculateButton_Click(object sender,EventArgs e)
{
// Declaring all variables
int monthsCounter;
double loan;
double rate;
double rebate;
double principal;
double balance;
int months = 0;
double principalPayment = 0;
double pmt = 0;
double interest = 0;
double totalInterest = 0;
double totalPrincipal = 0;
double totalPayment = 0;
double monthlyRate;
try
{
// Parse data from textboxes
double.TryParse(loanAmountBox.Text,out loan);
double.TryParse(annualAPRBox.Text,out rate);
double.TryParse(rebateBox.Text,out rebate);
// Check which loan month radio button is selected
if (loan6Months.Checked)
{
months = 6;
}
else if (loan12Months.Checked)
{
months = 12;
}
else if (loan18Months.Checked)
{
months = 18;
}
else if (loans24Months.Checked)
months = 24;
// Validates if the Loan Amount textbox is blank and if so,throws an error message pop up
if (loan == 0)
{
MessageBox.Show("Please enter a loan value.","Error Message",MessageBoxButtons.OK,MessageBoxIcon.Error);
loanAmountBox.Focus();
loanAmountBox.SelectAll();
}
else if (rate == 0)
{
MessageBox.Show("Please enter/select an APR value.",MessageBoxIcon.Error);
annualAPRBox.Focus();
annualAPRBox.SelectAll();
}
rate = (rate / 100) / 12;
loan = loan - rebate;
// Listbox loop
for (monthsCounter = 1; monthsCounter <= months; monthsCounter = monthsCounter + 1)
{
// Add to total variables
totalInterest += interest;
totalPrincipal += principalPayment;
totalPayment += pmt;
// Calculate the principal payment
interest = loan * rate;
principalPayment = (loan * rate * Math.Pow(1 + rate,months)) / (Math.Pow(1 + rate,months) - 1);
pmt = principalPayment + interest;
loan = loan - principalPayment;
// Output data to listbox
loanListBox.Items.Add(String.Format("{0,5}{1,12}{2,12}{3,12}{4,12}",monthsCounter,interest.ToString("N2"),principalPayment.ToString("N2"),pmt.ToString("N2"),loan.ToString("N2")));
}
loanListBox.Items.Add("");
loanListBox.Items.Add(String.Format("{0,"Total",totalInterest.ToString("N2"),totalPrincipal.ToString("N2"),totalPayment.ToString("N2")));
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
}
这是运行程序时输出的内容:
但是,输出应该是这样的:
解决方法
您用于摊销的公式是正确的,这是您执行此操作后得出的错误结果。 principalPayment
是包括利息的总付款。您可能应该将其重命名为totalPayment
,因为该名称具有误导性。如果您知道总付款额,并且知道利息。您将如何获得本金?
Interest + Principal = Total Payment
更新: 请记住,摊销公式中使用的贷款价值不会更改-原始贷款金额将用于所有计算。
任何时候看到“ ???”,都意味着您需要填写代码。
您可以考虑添加一个变量:
double originalLoanAmount = 0;
然后将贷款金额放入originalLoanAmount
。
double.TryParse(loanAmountBox.Text,out originalLoanAmount);
在“ for”循环之前设置初始值:
originalLoanAmount = ???
balance = ???
您如何计算利息?
interest = ???
计算总付款额:(原始贷款金额不变)
pmt = (originalLoanAmount * rate * Math.Pow(1 + rate,months)) / (Math.Pow(1 + rate,months) - 1);
什么是本金付款?
principalPayment = ???
新的余额是多少?
balance = ???
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。