对于计算错误的Next Loop?

时间:2014-11-18 03:40:20

标签: vb.net loops

我正在尝试在Visual Basic 2010中构建一个程序,将每小时工资计算为年度数字(例如10 * 40 * 52),并将该年度数字的百分比增加并反映超过10年。我的主要问题是,当我到达下一个循环时,它应该迭代数学9次(第一次是固定的),我发现代码存储了第一次迭代的值并在接下来的8次迭代中使用它。我该如何防止这种情况发生。请记住,我是编程的新手,试图保持简单。谢谢!

option Strict On

Public Class frmPayCalculator

Private Sub btnComputeFuturePay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnComputeFuturePay.Click
    ' The btnComputeFuturePay event accepts accepts hourly wage and pay raise
    ' information, and then displays the yearly pay over the next ten years.

    Dim decHourlyWage As Decimal
    Dim decYearlyWageComputed As Decimal
    Dim decExpectedRaise As Decimal
    Dim decExpectedRaiseComputed As Decimal
    Dim intYears As Integer
    Dim decIncrementedYearlyWage As Decimal

    If IsNumeric(txtHourlyWage.Text) Then
        ' Convert input to decimal.
        decHourlyWage = Convert.ToDecimal(txtHourlyWage.Text)
        ' Data validation to check if input is a positive.
        If decHourlyWage > 0 Then
            decYearlyWageComputed = decHourlyWage * 40 * 52
            lstDecadeWage.Items.Add("Year 1" & " - Wage: " & decYearlyWageComputed.ToString("C"))
        Else
            ' Error for negative number
            lstDecadeWage.Items.Clear()
            txtHourlyWage.Focus()
            MsgBox("You have entered a negative number for Hourly' Data validation to check if input is a number. Rate, try again with a positive number", , "Input Error")
        End If
        ' Data validation to check if input is a number.
        If IsNumeric(txtExpectedRaise.Text) Then
            ' Convert Data to decimal
            decExpectedRaise = Convert.ToDecimal(txtExpectedRaise.Text)
            ' Divide the whole number by 100 to make it a percent.
            decExpectedRaiseComputed = decExpectedRaise / 100
        Else
            ' Error for non-number: Expected Raise percent.
            lstDecadeWage.Items.Clear()
            txtHourlyWage.Focus()
            MsgBox("You have entered a non-number for the Expected Raise, try again with a number.", , "Input Error")
        End If
    Else
        ' Error for non-number: Hourly Rate.
        lstDecadeWage.Items.Clear()
        txtHourlyWage.Focus()
        MsgBox("You have entered a non-number for Hourly Rate, try again with a number.", , "Input Error")
    End If

    For intYears = 2 To 10
        decIncrementedYearlyWage += (decYearlyWageComputed + decYearlyWageComputed * decExpectedRaiseComputed)
        lstDecadeWage.Items.Add("Year " & intYears & " - Wage: " & decIncrementedYearlyWage.ToString("c"))
    Next

End Sub

Private Sub mnuClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClear.Click
    ' The mnuClear click event that clears the ListBox object, Wage and Raise Textboxes and, enables the Compute
    ' Future Pay button.

End Sub

Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
    ' The mnuExit click event closes the window and exits the application.
    Close()

End Sub


Private Sub frmPayCalculator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Preload event that clears the ListBox object, Wage and Raise Textboxes and, enables the Compute
    ' Future Pay button.

End Sub
End Class

2 个答案:

答案 0 :(得分:0)

而不是:

decIncrementedYearlyWage += (decYearlyWageComputed + decYearlyWageComputed * decExpectedRaiseComputed)

每次迭代会得到相同的结果,因为公式中的值不会改变,你可能会找到这样的东西:

decIncrementedYearlyWage += (decIncrementedYearlyWage * decExpectedRaiseComputed)

答案 1 :(得分:0)

例如:HourlyWage = 5,ExpectedRaise = 10

使用这些数据,如果您尝试执行以下操作:

Year 1 - Wage: $ 10,400.00
Year 2 - Wage: $ 11,440.00
Year 3 - Wage: $ 12,584.00
Year 4 - Wage: $ 13,842.40
Year 5 - Wage: $ 15,226.64
Year 6 - Wage: $ 16,749.30
Year 7 - Wage: $ 18,424.23
Year 8 - Wage: $ 20,266.66
Year 9 - Wage: $ 22,293.32
Year 10 - Wage: $ 24,522.66

然后你必须这样做:

For intYears = 2 To 10
    decYearlyWageComputed += decYearlyWageComputed * decExpectedRaiseComputed
    lstDecadeWage.Items.Add("Year " & intYears & " - Wage: " & decYearlyWageComputed.ToString("c"))
Next