VB按揭计算器无法显示正确的输出

时间:2016-10-12 02:50:54

标签: vb.net

我的教授要求我们使用子程序创建抵押计算器。我能够在一键式点击功能中完成此任务,而无需使用子或ByVal / ByRef。然而,当我把它分成几个不同的函数和sub我有0个错误但是当我去点击按钮并计算时,3个显示输出都是0.我假设它是我的Function calc块中的东西或者在我的btnCalculate_Click Sub。

Public Class frmMortgage

    Dim annualRateOfInterest, monthlyPayment, begBalance As Double 'Declaring variables
    Dim intForMonth, redOfPrincipal, endBalance As Double

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

        Call getInput(annualRateOfInterest, monthlyPayment, begBalance)

        Call displayOutput(intForMonth, redOfPrincipal, endBalance)

    End Sub

    Sub getInput(ByVal annualRateOfInterests As Double, ByVal monthlyPayment As Double, ByVal begBalance As Double) 'Values input into txt boxes
        annualRateOfInterest = CDbl(txtInterest.Text) / 100 'Change number entered into a percentage
        monthlyPayment = CDbl(txtPayment.Text)
        begBalance = CDbl(txtBalance.Text)
    End Sub

    Function calc(ByVal annualRateOfInterest As Double, ByVal monthlyPayment As Double, ByVal begBalanceintForMonth As Double, ByVal redOfPrincipal As Double, ByVal endBalance As Double)
        Return intForMonth = (annualRateOfInterest / 12) * begBalance 'Take percentage divided by 12 times beginning balance
        Return redOfPrincipal = monthlyPayment - intForMonth
        Return endBalance = begBalance - redOfPrincipal
    End Function

    Sub displayOutput(ByRef intForMonth As Double, ByRef redOfPrincipal As Double, ByRef endBalance As Double)
        mtbMonth.Text = CDbl(intForMonth) 'Display the calculated data into correct textboxes.
        mtbROP.Text = CDbl(redOfPrincipal)
        mtbEndBalance.Text = CDbl(endBalance)
    End Sub

End Class

1 个答案:

答案 0 :(得分:0)

我只是将其合并为一个Sub

Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCalculate.Click
    ComputeMortgage(Val(txtInterest.Text), Val(txtPayment.Text), Val(txtBalance.Text))
End Sub

Sub ComputeMortgage(ByVal Interest As Double, ByVal monthlyPayment As Double, ByVal begBalance As Double)
    annualRateOfInterest = Interest / 100

    'COMPUTATION
    intForMonth = (annualRateOfInterest / 12) * begBalance 'Take percentage divided by 12 times beginning balance
    redOfPrincipal = monthlyPayment - intForMonth
    endBalance = begBalance - redOfPrincipal

    'DISPLAYING
    mtbMonth.Text = intForMonth 'Display the calculated data into correct textboxes.
    mtbROP.Text = redOfPrincipal
    mtbEndBalance.Text = endBalance

End Sub