首页 > 解决方案 > 车辆贷款计算器 - 我的程序无法输出正确的值

问题描述

我一直在尝试让我的摊销计算器工作,但是期末付款余额不会以 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.", "Error Message", MessageBoxButtons.OK, 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,5}{1,12}{2,12}{3,12}", "Total", totalInterest.ToString("N2"), totalPrincipal.ToString("N2"), totalPayment.ToString("N2")));
        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message);
        }
    }
}

这是我运行程序时的输出: 我的程序

但是,输出应该是这样的: 正确的输出

标签: c#calculatoramortization

解决方案


您用于摊销的公式是正确的,这是您在此之后所做的事情会给您带来错误的结果。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 = ???

推荐阅读