我需要一些帮助来创建抵押贷款计算器,允许用户输入贷款金额,贷款年限和贷款利率,并计算每月抵押贷款支付,并在datagridview控件中显示每月到期的金额。问题是,我似乎无法使用for循环来循环它,我需要使用循环来显示所有的每月付款,但我尝试它只是每个月重复相同的金额。这是我的代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim loanAmount As Double
Dim loanYears As Integer
Dim loanRate As Double
Dim monthlyPayment As Double
Dim numberofPayments As Integer
Dim monthlyInterest As Double
Dim decimalInterest As Double
Dim interestPaid As Double
Dim newBalance As Double
Dim principle As Double
Dim n As Integer
Dim Pmt As Integer = 1
loanAmount = Val(TextBox1.Text)
loanYears = Val(TextBox2.Text)
loanRate = Val(TextBox3.Text)
decimalInterest = loanRate / 100
numberofPayments = loanYears * 12
monthlyInterest = decimalInterest / 12
Label1.Text = monthlyPayment
For Pmt = 1 To numberofPayments
n = n + 1
If n = numberofPayments + 1 Then
Exit For
End If
monthlyPayment = loanAmount * monthlyInterest / (1 - (1 + monthlyInterest) ^ -numberofPayments)
interestPaid = loanAmount * monthlyInterest
principle = monthlyPayment - interestPaid
newBalance = loanAmount - principle
Me.DataGridView1.Rows.Add(n, Math.Round(monthlyPayment, 2), Math.Round(interestPaid, 2), Math.Round(principle, 2), Math.Round(newBalance, 2))
Next
End Sub
如果有人可以帮助我,我将不胜感激。 谢谢
答案 0 :(得分:0)
loanAmount = loanAmount - principle
内部需要For
,或跟踪原则平衡的其他一些语句,并在每次迭代中使用它。看起来你正试图用newBalance变量来做这件事,但是每次迭代状态都超过了loanAmount,它永远不会减少,因此值永远不会改变。我建议您跟踪数学并确保您的算法实际执行您想要的操作。