-2

我正在编写一个程序,该程序应该使用给定信息计算余额(他们选择哪个复选框(储蓄(7%),公司(5%),或两者,然后他们选择的年份(2015,2016,2017),最后他们在提示输入余额时投入的金额(这取决于他们在第一步中选择的账户类型)。除了他们选择两种账户类型(储蓄和公司)。对我来说,如果两者都被选中,它们应该是一条只添加两个总数的行,但事实并非如此。至少对我来说不是。

如果我输入 2000 美元的储蓄余额并选择 2015 年,我得到的总额是 2289.80 美元,这是正确的,然后如果我为公司账户选择 2015 年并输入 1000 美元,那么总额是 1102.50 美元。因此,如果我选择了两个帐户并输入了相同的数字,但同时选中了两个复选框,我将得到 3434.70 美元,但我应该得到 3392.30 美元!

这是我的代码,任何帮助将不胜感激!(我认为它可能实际上只是一行代码,它正在杀死我!)

Public Class Form1

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

    Dim interestRate, initialBalanceSavings, initialBalanceCorporate, finalBalance, year, theYear, subTotal As Double
    Dim savingsInterestRate As Double = 0
    Dim corporateInterestRate As Double = 0
    Dim savingsSubTotal As Double = 0
    Dim corporateSubTotal As Double = 0

    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

        savingsInterestRate = 0.07

    ElseIf checkBoxCorporate.Checked Then

        corporateInterestRate = 0.05
    End If


    If checkBoxSavings.Checked = False And checkBoxCorporate.Checked = False Then

        MsgBox("Please chose an account type to proceed!", MsgBoxStyle.Critical, "Error")

    End If


    If radButton2015.Checked Then

        theYear = 2015

    ElseIf radButton2016.Checked Then

        theYear = 2016

    ElseIf 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


    savingsSubTotal = initialBalanceSavings
    corporateSubTotal = initialBalanceCorporate

    For year = 2013 To theYear - 1

        If savingsInterestRate > 0 Then

            savingsSubTotal = savingsSubTotal * (1 + savingsInterestRate)

        End If

        If corporateInterestRate > 0 Then

            corporateSubTotal = corporateSubTotal * (1 + corporateInterestRate)

        End If

    Next

    finalBalance = savingsSubTotal + corporateSubTotal
    txtBoxEstimatedBudget.Text = FormatCurrency(finalBalance)

End Sub
4

1 回答 1

3

我相信错误的逻辑出现在这段代码中:

finalBalance = initialBalanceSavings + initialBalanceCorporate

For year = 2013 To theYear - 1
    subTotal = finalBalance * (1 + interestRate)
    finalBalance = subTotal
Next

结合这段代码:

If checkBoxSavings.Checked Then
    interestRate = 0.07
ElseIf checkBoxCorporate.Checked Then
    interestRate = 0.05
End If

在上面,即使选中了两个复选框,利率也设置为 0.07,所以在你的循环中,如果有人计算 2015,你会做一些事情,比如给你一个最终的总数:3000 * 1.07 * 1.07,而你真的想要(2000 * 1.07 * 1.07) + (1000 * 1.05 * 1.05)

本质上,因为您只使用一个变量来保存利率(以及只有一个变量来保存小计),所以您的代码无法正确说明有人同时选择两个帐户的情况。我建议有两个变量来保持利率:

Dim savingsInterestRate as Double = 0
Dim corporateInterestRate as Double = 0

和 2 个变量来保存小计:

Dim savingsSubTotal as Double = 0
Dim corporateSubTotal as Double = 0

在你的 For 循环中做:

    savingsSubTotal = initialBalanceSavings
    corporateSubTotal = initialBalanceCorporate

    For year = 2013 To theYear - 1
        If savingsInterestRate > 0 Then
            savingsSubTotal = savingsSubTotal * (1 + savingsInterestRate)
        End If

        If corporateInterestRate > 0 Then
            corporateSubTotal = corporateSubTotal * (1 + corporateInterestRate)
        End If
    Next

    finalBalance = savingsSubTotal + corporateSubTotal

并将您的利率支票更改为:

If checkBoxSavings.Checked Then
    savingsInterestRate = 0.07
End If

If checkBoxCorporate.Checked Then
    corporateInterestRate = 0.05
End If

所以你可以考虑这个人的选择(使用ElseIf如果他们同时检查两者的方法,它将放弃公司利率,因为checkBoxSavings.Checked它已经是真实的,它永远不会落入ElseIf块中)

于 2013-03-26T02:03:01.587 回答