程序返回0表示值

时间:2014-02-28 01:51:37

标签: vb.net visual-studio-2010 vba

所以我要写一个程序来计算信用卡对帐单的余额和最低付款额。该程序应使用如下所示的事件过程。财务费用是旧余额的1.5%。如果新余额为20美元或更低,则最低付款额应为全部新余额。否则,最低付款额应为20美元加上20美元以上新余额的10%。事件程序是:

Private Sub btnCalcBal_Paymt_Click(sender As Object, e As EventArgs) Handles btnCalcBal_Paymt.Click
    Dim oldBalance, charges, credits, newBalance, minPayment As Double
    InputData(oldBalance, charges, credits)
    CalculateNewValues(oldBalance, charges, credits, newBalance, minPayment)
    DisplayData(newBalance, minPayment)
End Sub

Sub InputData(oldBalance As Double, charges As Double, credits As Double)
    oldBalance = CDbl(txtOldBal.Text)
    charges = CDbl(txtCharges.Text)
    credits = CDbl(txtCredits.Text)
End Sub
Function CalculateNewValues(oldBalance As Double, newBalance As Double, minPayment As Double, charges As Double, credits As Double) As Double
    Dim rate As Double = 1.015
    newBalance = (rate * oldBalance) + charges - credits
    Return newBalance
    If newBalance <= 20 Then
        minPayment = newBalance
    Else
        Return minPayment + (0.1 * newBalance)
    End If
End Function

Sub DisplayData(newBalance As Double, minPayment As Double)
    txtNewBal.Text = newBalance.ToString("c")
    txtMinPmt.Text = minPayment.ToString("c")
End Sub

当我编译时,我得到零结果。无法弄清楚为什么。

3 个答案:

答案 0 :(得分:0)

您的问题是您没有正确修改变量。将args传递给子函数的方法不会修改btnCalcBal_Paymt_Click方法中的值。您可以使用Function返回所需的值,但这样做的方式会更加困难。最后,您可以在参数中使用ByRef,以便变量通过引用传递给子函数,而不是 value (请参阅here

Private Sub btnCalcBal_Paymt_Click(sender As Object, e As EventArgs) Handles btnCalcBal_Paymt.Click
    Dim oldBalance, charges, credits, newBalance, minPayment As Double
    InputData(oldBalance, charges, credits)
    CalculateNewValues(oldBalance, charges, credits, newBalance, minPayment)
    DisplayData(newBalance, minPayment)
End Sub

Sub InputData(ByRef oldBalance As Double, ByRef charges As Double, ByRef credits As Double)
    oldBalance = CDbl(txtOldBal.Text)
    charges = CDbl(txtCharges.Text)
    credits = CDbl(txtCredits.Text)
End Sub
...

请告诉我这是否有帮助!

答案 1 :(得分:0)

您正在使用太多功能来完成相当简单的任务。您可以在单个事件处理程序中执行所需的任何操作。像:

Private Sub btnCalcBal_Paymt_Click(sender As Object, e As EventArgs) Handles btnCalcBal_Paymt.Click
    Dim oldBalance, charges, credits, newBalance, minPayment As Double
    oldBalance = CDbl(txtOldBal.Text)
    charges = CDbl(txtCharges.Text)
    credits = CDbl(txtCredits.Text)

    Dim rate As Double = 1.015
    newBalance = (rate * oldBalance) + charges - credits
    If newBalance <= 20 Then
        minPayment = newBalance
    Else
        minPayment = minPayment + (0.1 * newBalance)
    End If

    txtNewBal.Text = newBalance.ToString("c")
    txtMinPmt.Text = minPayment.ToString("c")
End Sub

答案 2 :(得分:0)

我意识到为时已晚,但也许其他苦苦挣扎的学生可以使用它。这有效,它完成了本书要求你做的事情。

Public Class Form1


Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click


    Dim oldBalance, charges, credits, newBalance, minPayment As Double
    InputData(oldBalance, charges, credits)
    CalculateNewValues(oldBalance, charges, credits, newBalance, minPayment)
    DisplayData(newBalance, minPayment)


End Sub

Sub InputData(ByRef oldBalance As String, ByRef charges As String, ByRef credits As String)
    oldBalance = CDbl(txtOldBal.Text)
    charges = CDbl(txtCharges.Text)
    credits = CDbl(txtCredits.Text)
End Sub

Sub CalculateNewValues(ByRef oldBalance As Double, ByRef charges As Double, ByRef credits As Double, ByRef newBalance As Double, ByRef minPayment As Double)
    Dim rate As Double = 1.015
    newBalance = (rate * oldBalance) + charges - credits
    If newBalance <= 20 Then
        minPayment = newBalance
    Else
        minPayment = ((newBalance - 20) * 0.1) + 20
    End If

End Sub

Sub DisplayData(newBalance As Double, minPayment As Double)
    lblMinPay.Text = minPayment.ToString("C")
    lblNewBal.Text = newBalance.ToString("C")
End Sub

End Class