Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 对于下一个计算错误的循环?_Vb.net_Loops - Fatal编程技术网

Vb.net 对于下一个计算错误的循环?

Vb.net 对于下一个计算错误的循环?,vb.net,loops,Vb.net,Loops,我试图在VisualBasic2010中构建一个程序,该程序将小时工资计算为一个年数(例如10*40*52),并在该年数的基础上增加百分比,并在10年内反映出来。我的主要问题是,当我进入for next循环时,它应该迭代数学9次(第一次是固定的),我发现代码存储了第一次迭代的值,并在接下来的8次迭代中使用该值。我怎样才能避免这种情况发生。请记住,我对编程还不熟悉,所以尽量保持简单。谢谢 option Strict On Public Class frmPayCalculator Privat

我试图在VisualBasic2010中构建一个程序,该程序将小时工资计算为一个年数(例如10*40*52),并在该年数的基础上增加百分比,并在10年内反映出来。我的主要问题是,当我进入for next循环时,它应该迭代数学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
而不是:

decIncrementedYearlyWage += (decYearlyWageComputed + decYearlyWageComputed * decExpectedRaiseComputed)
每次迭代都会得到相同的结果,因为公式中的值不变,您可能会寻找类似的结果:

decIncrementedYearlyWage += (decIncrementedYearlyWage * decExpectedRaiseComputed)

例如: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

我知道我在这方面遇到了一些逻辑错误。起初我不明白为什么这个解决方案有效,但它非常有意义。每次迭代运行时,都会存储迭代中的值,然后反复使用。因此列表框中的值会增加,但只会增加第一个值。