从按钮中的代码获得平衡

时间:2013-03-24 21:26:14

标签: vb.net

我在计算代码中的余额时遇到了一些麻烦。

在此计划中(balance = balance * (1 + interestRate)。问题是我们在2013年,将计算2015-2017年的余额,该余额由用户在程序中点击的复选框选取,并且用户也会选择他们将拥有的帐户类型(即储蓄:7%利息公司:5%利息)。

然后,他们会点击mediaEstimatedFund按钮,并要求他们输入所选帐户类型的余额( Savings,Corporate 或两者)。然后,它应计算他们选择的(帐户余额金额)的余额。

以下是我的代码,并没有给我正确的余额(例如:如果用户选择$ 2000.00,储蓄帐户为7%和2016年,它应该给我估计预算为2621.60美元),但事实并非如此!

Year     Savings Account                Corporate Account         Saving + Corporate    

2013       $2000.00                  $1000.00                   $3000.00
2014       $2140.00                  $1050.00                   $3190.00
2015       $2289.8                   $1102.5                    $3392.3
2016       $2450.086                 $1157.625                  $3607.711      
2017       $2621.59202               $1215.50625                $3837.09827

Public Class Form1

Private Sub btnMediaEstimatedFund_Click(sender As Object, e As EventArgs) Handles btnMediaEstimatedFund.Click

    Dim interestRate, initialBalanceSavings, initialBalanceCorporate, finalBalance, theYear, balance, subTotal As Double

    txtBoxEstimatedBudget.Enabled = False
    txtBoxAgenciesNeeded.Enabled = False

    If radButtonTraditional.Checked Then

        txtBoxAgenciesNeeded.Text = 3

    ElseIf radButtonEMedia.Checked Then

        txtBoxAgenciesNeeded.Text = 2

    End If

    If checkBoxSavings.Checked Then

        interestRate = 0.07

    ElseIf checkBoxCorporate.Checked Then

        interestRate = 0.05

    ElseIf checkBoxCorporate.Checked And checkBoxSavings.Checked Then

        interestRate = 0.12

    End If

    If radButton2015.Checked Then

        theYear = 2015

    End If

    If radButton2016.Checked Then

        theYear = 2016

    End If

    If radButton2017.Checked Then

        theYear = 2017

    End If

    Dim inputtedData As String

    If checkBoxSavings.Checked Then
        Do

            inputtedData = InputBox("Please enter a balance for SAVINGS account between $500.00 and $3000.00", "Initial Savings Balance", "0.00")

            If inputtedData = "" Then

                MsgBox("User chose to cancel calculation!")

                Exit Sub

            Else

                initialBalanceSavings = CType(inputtedData, Single)

                If initialBalanceSavings > 3000 Or initialBalanceSavings < 500 Then MsgBox("Please enter a balance for SAVINGS account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")

            End If

        Loop Until initialBalanceSavings >= 500 And initialBalanceSavings <= 3000

    End If

    If checkBoxCorporate.Checked Then

        Do

            inputtedData = InputBox("Please enter a balance for CORPORATE account between $500.00 and $3000.00", "Initial Corporate Balance", "0.00")

            If inputtedData = "" Then

                MsgBox("User chose to Cancel calculation!")

                Exit Sub

            Else

                initialBalanceCorporate = CType(inputtedData, Single)

                If initialBalanceCorporate > 3000 Or initialBalanceCorporate < 500 Then MsgBox("Please enter a balance for CORPORATE account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")

            End If

        Loop Until initialBalanceCorporate >= 500 And initialBalanceCorporate <= 3000

    End If

    finalBalance = initialBalanceSavings + initialBalanceCorporate

    For theYear = 2013 To 2017

        subTotal = finalBalance * (1 + interestRate)
        finalBalance = subTotal

    Next

    txtBoxEstimatedBudget.Text = FormatCurrency(finalBalance)

End Sub

2 个答案:

答案 0 :(得分:1)

For TheYear = 2013 to 2016
  SubTotal = balance * (1 + interestRate)
  balance = SubTotal
Next

更新

上面的代码块只是为了给你一个如何做循环的IDEA以及在循环中应该计算什么。要获得实际答案,您必须记住2013年是当前年份,因此您不会实际计算该年度的利息。你实现这一目标的关键是循环的格式化,实际上应该从下一年开始。

For Year = 2013 + 1 to 2016
  'blah blah blah
Next

答案 1 :(得分:1)

我不允许编辑Zafs的答案以包含额外的行,所以这是我的看法。重要的是,finalBalance的初始化是for循环的

finalBalance = initialBalanceSavings + initialBalanceCorporate
For theYear = 2013 To 2016
  subtotal = finalBalance * (1 + interestRate)
  finalBalance = subTotal
Next