0
我是第一年的編程學生,正在努力製作按揭付款計算器。用戶輸入他們的貸款金額和利率,選擇15或30年的期限長度,他們的每月付款應計算。我認爲數學是正確的,但我認爲我遇到了轉換數據類型的問題。我能夠計算的唯一結果是「無限」。我也試着將所有變量聲明爲雙精度,但沒有區別。由於Visual Basic中的按揭付款計算中收到「infinity」
下面是我遇到的一段代碼,Option Strict On包含在完整的代碼中。任何提示將非常感謝!
Dim decLoanAmount As Decimal
Dim decInterestRate As Decimal
Dim decFifteen As Decimal = 180D
Dim decThirty As Decimal = 360D
Dim decNumberOfMonths As Decimal
Dim dblPayment As Double
Dim decPayment As Decimal
' Did user enter a numeric value?
If IsNumeric(txtLoanAmount.Text) And IsNumeric(txtInterestRate.Text) Then
decLoanAmount = Convert.ToDecimal(txtLoanAmount.Text)
decInterestRate = Convert.ToDecimal(txtInterestRate.Text)
' Is Loan Amount greater than zero?
If decLoanAmount > 0 And decInterestRate > 0 Then
If radFifteen.Checked = True Then
decNumberOfMonths = decFifteen
ElseIf radThirty.Checked = True Then
decNumberOfMonths = decThirty
End If
' Calculate the monthly payments as a double
dblPayment = (decLoanAmount * (decInterestRate/12/100) * (1 + (decInterestRate/12/100) _
^decNumberOfMonths))/((1 + (decInterestRate/12/100)^decNumberOfMonths) - 1)
' Convert double to decimal
decPayment = Convert.ToDecimal(decPayment)
' Display monthly payment
lblPayment.Text = decPayment.ToString("C2")
Else
If decLoanAmount < 0 Then
MsgBox("Please enter a valid loan amount.", , "Input error")
End If
If decInterestRate < 0 Then
MsgBox("Please enter a valid interest rate.", , "Input error")
End If
End If
Else
' Display error message if user entered a negative value.
MsgBox("Please enter a numeric value.", , "Input Error")
End If
End Sub
我建議保理出來了一下,檢查值就像你所期望的那樣 - 例如,在計算過程中會重複'(decInterestRate/12/100)',所以爲什麼不先計算它,然後檢查這個值是否如你所期望的那樣。 – 2014-12-07 21:08:49
此外,'decPayment = Convert.ToDecimal(decPayment)'不會做很多... – 2014-12-07 21:09:59
您不希望人們在這裏爲您編寫和/或調試代碼,對吧?向我們展示一些研究,提供樣本輸入,輸出,更多解釋。假設上面的代碼不在那裏,你認爲你描述的內容足以幫助你嗎? – Neolisk 2014-12-07 21:48:08