我正在编写一个应该用给定信息计算余额的程序(他们选择哪个复选框(储蓄(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
答案 0 :(得分: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,你会做一些事情,比如给你一个最终总数:{{1但是你真的想要3000 * 1.07 * 1.07
本质上,因为您只使用一个变量来保持利率(以及只有一个变量来保存小计),您的代码无法正确考虑某人选择这两个帐户的情况。我建议有2个变量来保持利率:
(2000 * 1.07 * 1.07) + (1000 * 1.05 * 1.05)
用于保存小计的2个变量:
Dim savingsInterestRate as Double = 0
Dim corporateInterestRate as Double = 0
在For循环中执行:
Dim savingsSubTotal as Double = 0
Dim corporateSubTotal as Double = 0
并将您的利率检查更改为:
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
已经是真的,它永远不会落入{{1阻止)