Я пытаюсь создать программу на 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
Я знал, что я бегу в некоторой логической ошибки на моем конце. Сначала я не понимал, почему решение работает, но это имеет смысл. Каждый раз, когда итерация выполнялась, значение с итерации сохранялось, а затем использовалось снова и снова. Таким образом, значения в окне списка будут увеличиваться, но только по первому значению. – kl654